{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 统一测试\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "=== 测试 GLM-4 ===\n",
      "\n",
      "测试 glm-4 对话模型...\n",
      "text: 你好\n",
      "text: ，\n",
      "text: 我是\n",
      "text: 智\n",
      "text: 谱\n",
      "text: 清\n",
      "text: 言\n",
      "text: ，\n",
      "text: 是\n",
      "text: 清华大学\n",
      "text:  K\n",
      "text: EG\n",
      "text:  实\n",
      "text: 验\n",
      "text: 室\n",
      "text: 和\n",
      "text: 智\n",
      "text: 谱\n",
      "text:  AI\n",
      "text:  公司\n",
      "text: 于\n",
      "text:  \n",
      "text: 202\n",
      "text: 3\n",
      "text:  年\n",
      "text: 共同\n",
      "text: 训练\n",
      "text: 的语言\n",
      "text: 模型\n",
      "text: 。\n",
      "text: 我的\n",
      "text: 目标\n",
      "text: 是通过\n",
      "text: 回答\n",
      "text: 用户\n",
      "text: 提出\n",
      "text: 的问题\n",
      "text: 来\n",
      "text: 帮助他们\n",
      "text: 解决问题\n",
      "text: 。\n",
      "text: 由于\n",
      "text: 我是一个\n",
      "text: 计算机\n",
      "text: 程序\n",
      "text: ，\n",
      "text: 所以\n",
      "text: 我没有\n",
      "text: 自我\n",
      "text: 意识\n",
      "text: ，\n",
      "text: 也不能\n",
      "text: 像\n",
      "text: 人类\n",
      "text: 一样\n",
      "text: 感知\n",
      "text: 世界\n",
      "text: 。\n",
      "text: 我只能\n",
      "text: 通过\n",
      "text: 分析\n",
      "text: 我所\n",
      "text: 学\n",
      "text: 到的\n",
      "text: 信息\n",
      "text: 来\n",
      "text: 回答\n",
      "text: 问题\n",
      "text: 。\n",
      "text: \n",
      "\n",
      "=== 测试 GLM-4V ===\n",
      "\n",
      "测试 glm-4v-plus 图像URL模型...\n",
      "\n",
      "=== 测试结果统计 ===\n",
      "总计测试: 1 项\n",
      "\n",
      "成功: 1 项\n",
      "成功的测试:\n",
      "  - glm-4-对话\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mConnectionRefusedError\u001b[0m                    Traceback (most recent call last)",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\site-packages\\urllib3\\util\\connection.py:73\u001b[0m, in \u001b[0;36mcreate_connection\u001b[1;34m(address, timeout, source_address, socket_options)\u001b[0m\n\u001b[0;32m     72\u001b[0m     sock\u001b[38;5;241m.\u001b[39mbind(source_address)\n\u001b[1;32m---> 73\u001b[0m \u001b[43msock\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconnect\u001b[49m\u001b[43m(\u001b[49m\u001b[43msa\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     74\u001b[0m \u001b[38;5;66;03m# Break explicitly a reference cycle\u001b[39;00m\n",
      "\u001b[1;31mConnectionRefusedError\u001b[0m: [WinError 10061] 由于目标计算机积极拒绝，无法连接。",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[9], line 338\u001b[0m\n\u001b[0;32m    335\u001b[0m         tester\u001b[38;5;241m.\u001b[39mprint_test_results()\n\u001b[0;32m    337\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m__main__\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m--> 338\u001b[0m     \u001b[43mmain\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "Cell \u001b[1;32mIn[9], line 278\u001b[0m, in \u001b[0;36mmain\u001b[1;34m()\u001b[0m\n\u001b[0;32m    276\u001b[0m \u001b[38;5;66;03m# 测试GLM-4V\u001b[39;00m\n\u001b[0;32m    277\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m=== 测试 GLM-4V ===\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m--> 278\u001b[0m \u001b[43mtester\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtest_model\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mglm-4v-plus\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m图像URL\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m    279\u001b[0m \u001b[43m                 \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m这张图片是什么内容？\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m    280\u001b[0m \u001b[43m                 \u001b[49m\u001b[43murl\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtester\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtest_urls\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mimage\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    281\u001b[0m tester\u001b[38;5;241m.\u001b[39mtest_model(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mglm-4v-plus\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m图像文件\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[0;32m    282\u001b[0m                  \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m描述这张图片\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m    283\u001b[0m                  file_path\u001b[38;5;241m=\u001b[39mtester\u001b[38;5;241m.\u001b[39mlocal_files[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mimage\u001b[39m\u001b[38;5;124m'\u001b[39m])\n\u001b[0;32m    285\u001b[0m \u001b[38;5;66;03m# 测试文生图\u001b[39;00m\n",
      "Cell \u001b[1;32mIn[9], line 70\u001b[0m, in \u001b[0;36mAITester.test_model\u001b[1;34m(self, model_name, test_type, content, file_path, url)\u001b[0m\n\u001b[0;32m     68\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m测试 \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mmodel_name\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mtest_type\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m模型...\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m     69\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m---> 70\u001b[0m     response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msession\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpost\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbase_url\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdata\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfiles\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfiles\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[0;32m     71\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_process_response(response)\n\u001b[0;32m     72\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtest_results[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124msuccess\u001b[39m\u001b[38;5;124m'\u001b[39m]\u001b[38;5;241m.\u001b[39mappend(test_id)\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\site-packages\\requests\\sessions.py:637\u001b[0m, in \u001b[0;36mSession.post\u001b[1;34m(self, url, data, json, **kwargs)\u001b[0m\n\u001b[0;32m    626\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mpost\u001b[39m(\u001b[38;5;28mself\u001b[39m, url, data\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, json\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[0;32m    627\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124;03m\"\"\"Sends a POST request. Returns :class:`Response` object.\u001b[39;00m\n\u001b[0;32m    628\u001b[0m \n\u001b[0;32m    629\u001b[0m \u001b[38;5;124;03m    :param url: URL for the new :class:`Request` object.\u001b[39;00m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    634\u001b[0m \u001b[38;5;124;03m    :rtype: requests.Response\u001b[39;00m\n\u001b[0;32m    635\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n\u001b[1;32m--> 637\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mPOST\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdata\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mjson\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mjson\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\site-packages\\requests\\sessions.py:589\u001b[0m, in \u001b[0;36mSession.request\u001b[1;34m(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)\u001b[0m\n\u001b[0;32m    584\u001b[0m send_kwargs \u001b[38;5;241m=\u001b[39m {\n\u001b[0;32m    585\u001b[0m     \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtimeout\u001b[39m\u001b[38;5;124m\"\u001b[39m: timeout,\n\u001b[0;32m    586\u001b[0m     \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mallow_redirects\u001b[39m\u001b[38;5;124m\"\u001b[39m: allow_redirects,\n\u001b[0;32m    587\u001b[0m }\n\u001b[0;32m    588\u001b[0m send_kwargs\u001b[38;5;241m.\u001b[39mupdate(settings)\n\u001b[1;32m--> 589\u001b[0m resp \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msend\u001b[49m\u001b[43m(\u001b[49m\u001b[43mprep\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43msend_kwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    591\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m resp\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\site-packages\\requests\\sessions.py:703\u001b[0m, in \u001b[0;36mSession.send\u001b[1;34m(self, request, **kwargs)\u001b[0m\n\u001b[0;32m    700\u001b[0m start \u001b[38;5;241m=\u001b[39m preferred_clock()\n\u001b[0;32m    702\u001b[0m \u001b[38;5;66;03m# Send the request\u001b[39;00m\n\u001b[1;32m--> 703\u001b[0m r \u001b[38;5;241m=\u001b[39m \u001b[43madapter\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msend\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    705\u001b[0m \u001b[38;5;66;03m# Total elapsed time of the request (approximately)\u001b[39;00m\n\u001b[0;32m    706\u001b[0m elapsed \u001b[38;5;241m=\u001b[39m preferred_clock() \u001b[38;5;241m-\u001b[39m start\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\site-packages\\requests\\adapters.py:667\u001b[0m, in \u001b[0;36mHTTPAdapter.send\u001b[1;34m(self, request, stream, timeout, verify, cert, proxies)\u001b[0m\n\u001b[0;32m    664\u001b[0m     timeout \u001b[38;5;241m=\u001b[39m TimeoutSauce(connect\u001b[38;5;241m=\u001b[39mtimeout, read\u001b[38;5;241m=\u001b[39mtimeout)\n\u001b[0;32m    666\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 667\u001b[0m     resp \u001b[38;5;241m=\u001b[39m \u001b[43mconn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43murlopen\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    668\u001b[0m \u001b[43m        \u001b[49m\u001b[43mmethod\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmethod\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    669\u001b[0m \u001b[43m        \u001b[49m\u001b[43murl\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    670\u001b[0m \u001b[43m        \u001b[49m\u001b[43mbody\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbody\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    671\u001b[0m \u001b[43m        \u001b[49m\u001b[43mheaders\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mheaders\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    672\u001b[0m \u001b[43m        \u001b[49m\u001b[43mredirect\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m    673\u001b[0m \u001b[43m        \u001b[49m\u001b[43massert_same_host\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m    674\u001b[0m \u001b[43m        \u001b[49m\u001b[43mpreload_content\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m    675\u001b[0m \u001b[43m        \u001b[49m\u001b[43mdecode_content\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m    676\u001b[0m \u001b[43m        \u001b[49m\u001b[43mretries\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmax_retries\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    677\u001b[0m \u001b[43m        \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtimeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    678\u001b[0m \u001b[43m        \u001b[49m\u001b[43mchunked\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mchunked\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    679\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    681\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m (ProtocolError, \u001b[38;5;167;01mOSError\u001b[39;00m) \u001b[38;5;28;01mas\u001b[39;00m err:\n\u001b[0;32m    682\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mConnectionError\u001b[39;00m(err, request\u001b[38;5;241m=\u001b[39mrequest)\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\site-packages\\urllib3\\connectionpool.py:789\u001b[0m, in \u001b[0;36mHTTPConnectionPool.urlopen\u001b[1;34m(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, preload_content, decode_content, **response_kw)\u001b[0m\n\u001b[0;32m    786\u001b[0m response_conn \u001b[38;5;241m=\u001b[39m conn \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m release_conn \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m    788\u001b[0m \u001b[38;5;66;03m# Make the request on the HTTPConnection object\u001b[39;00m\n\u001b[1;32m--> 789\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_make_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    790\u001b[0m \u001b[43m    \u001b[49m\u001b[43mconn\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    791\u001b[0m \u001b[43m    \u001b[49m\u001b[43mmethod\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    792\u001b[0m \u001b[43m    \u001b[49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    793\u001b[0m \u001b[43m    \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtimeout_obj\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    794\u001b[0m \u001b[43m    \u001b[49m\u001b[43mbody\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mbody\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    795\u001b[0m \u001b[43m    \u001b[49m\u001b[43mheaders\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mheaders\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    796\u001b[0m \u001b[43m    \u001b[49m\u001b[43mchunked\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mchunked\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    797\u001b[0m \u001b[43m    \u001b[49m\u001b[43mretries\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mretries\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    798\u001b[0m \u001b[43m    \u001b[49m\u001b[43mresponse_conn\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mresponse_conn\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    799\u001b[0m \u001b[43m    \u001b[49m\u001b[43mpreload_content\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpreload_content\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    800\u001b[0m \u001b[43m    \u001b[49m\u001b[43mdecode_content\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdecode_content\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    801\u001b[0m \u001b[43m    \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mresponse_kw\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    802\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    804\u001b[0m \u001b[38;5;66;03m# Everything went great!\u001b[39;00m\n\u001b[0;32m    805\u001b[0m clean_exit \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\site-packages\\urllib3\\connectionpool.py:495\u001b[0m, in \u001b[0;36mHTTPConnectionPool._make_request\u001b[1;34m(self, conn, method, url, body, headers, retries, timeout, chunked, response_conn, preload_content, decode_content, enforce_content_length)\u001b[0m\n\u001b[0;32m    492\u001b[0m \u001b[38;5;66;03m# conn.request() calls http.client.*.request, not the method in\u001b[39;00m\n\u001b[0;32m    493\u001b[0m \u001b[38;5;66;03m# urllib3.request. It also calls makefile (recv) on the socket.\u001b[39;00m\n\u001b[0;32m    494\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 495\u001b[0m     \u001b[43mconn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    496\u001b[0m \u001b[43m        \u001b[49m\u001b[43mmethod\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    497\u001b[0m \u001b[43m        \u001b[49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    498\u001b[0m \u001b[43m        \u001b[49m\u001b[43mbody\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mbody\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    499\u001b[0m \u001b[43m        \u001b[49m\u001b[43mheaders\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mheaders\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    500\u001b[0m \u001b[43m        \u001b[49m\u001b[43mchunked\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mchunked\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    501\u001b[0m \u001b[43m        \u001b[49m\u001b[43mpreload_content\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpreload_content\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    502\u001b[0m \u001b[43m        \u001b[49m\u001b[43mdecode_content\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdecode_content\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    503\u001b[0m \u001b[43m        \u001b[49m\u001b[43menforce_content_length\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43menforce_content_length\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    504\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    506\u001b[0m \u001b[38;5;66;03m# We are swallowing BrokenPipeError (errno.EPIPE) since the server is\u001b[39;00m\n\u001b[0;32m    507\u001b[0m \u001b[38;5;66;03m# legitimately able to close the connection after sending a valid response.\u001b[39;00m\n\u001b[0;32m    508\u001b[0m \u001b[38;5;66;03m# With this behaviour, the received response is still readable.\u001b[39;00m\n\u001b[0;32m    509\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBrokenPipeError\u001b[39;00m:\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\site-packages\\urllib3\\connection.py:441\u001b[0m, in \u001b[0;36mHTTPConnection.request\u001b[1;34m(self, method, url, body, headers, chunked, preload_content, decode_content, enforce_content_length)\u001b[0m\n\u001b[0;32m    439\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m header, value \u001b[38;5;129;01min\u001b[39;00m headers\u001b[38;5;241m.\u001b[39mitems():\n\u001b[0;32m    440\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mputheader(header, value)\n\u001b[1;32m--> 441\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mendheaders\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    443\u001b[0m \u001b[38;5;66;03m# If we're given a body we start sending that in chunks.\u001b[39;00m\n\u001b[0;32m    444\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m chunks \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\http\\client.py:1331\u001b[0m, in \u001b[0;36mHTTPConnection.endheaders\u001b[1;34m(self, message_body, encode_chunked)\u001b[0m\n\u001b[0;32m   1329\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m   1330\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m CannotSendHeader()\n\u001b[1;32m-> 1331\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_send_output\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmessage_body\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mencode_chunked\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mencode_chunked\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\http\\client.py:1091\u001b[0m, in \u001b[0;36mHTTPConnection._send_output\u001b[1;34m(self, message_body, encode_chunked)\u001b[0m\n\u001b[0;32m   1089\u001b[0m msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mb\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\r\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_buffer)\n\u001b[0;32m   1090\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_buffer[:]\n\u001b[1;32m-> 1091\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msend\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmsg\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1093\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m message_body \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m   1094\u001b[0m \n\u001b[0;32m   1095\u001b[0m     \u001b[38;5;66;03m# create a consistent interface to message_body\u001b[39;00m\n\u001b[0;32m   1096\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(message_body, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mread\u001b[39m\u001b[38;5;124m'\u001b[39m):\n\u001b[0;32m   1097\u001b[0m         \u001b[38;5;66;03m# Let file-like take precedence over byte-like.  This\u001b[39;00m\n\u001b[0;32m   1098\u001b[0m         \u001b[38;5;66;03m# is needed to allow the current position of mmap'ed\u001b[39;00m\n\u001b[0;32m   1099\u001b[0m         \u001b[38;5;66;03m# files to be taken into account.\u001b[39;00m\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\http\\client.py:1035\u001b[0m, in \u001b[0;36mHTTPConnection.send\u001b[1;34m(self, data)\u001b[0m\n\u001b[0;32m   1033\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msock \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m   1034\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mauto_open:\n\u001b[1;32m-> 1035\u001b[0m         \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconnect\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1036\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m   1037\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m NotConnected()\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\site-packages\\urllib3\\connection.py:279\u001b[0m, in \u001b[0;36mHTTPConnection.connect\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    278\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mconnect\u001b[39m(\u001b[38;5;28mself\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m--> 279\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msock \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_new_conn\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    280\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_tunnel_host:\n\u001b[0;32m    281\u001b[0m         \u001b[38;5;66;03m# If we're tunneling it means we're connected to our proxy.\u001b[39;00m\n\u001b[0;32m    282\u001b[0m         \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_has_connected_to_proxy \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\site-packages\\urllib3\\connection.py:199\u001b[0m, in \u001b[0;36mHTTPConnection._new_conn\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    194\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Establish a socket connection and set nodelay settings on it.\u001b[39;00m\n\u001b[0;32m    195\u001b[0m \n\u001b[0;32m    196\u001b[0m \u001b[38;5;124;03m:return: New socket connection.\u001b[39;00m\n\u001b[0;32m    197\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    198\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 199\u001b[0m     sock \u001b[38;5;241m=\u001b[39m \u001b[43mconnection\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcreate_connection\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    200\u001b[0m \u001b[43m        \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_dns_host\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mport\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    201\u001b[0m \u001b[43m        \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtimeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    202\u001b[0m \u001b[43m        \u001b[49m\u001b[43msource_address\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msource_address\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    203\u001b[0m \u001b[43m        \u001b[49m\u001b[43msocket_options\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msocket_options\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    204\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    205\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m socket\u001b[38;5;241m.\u001b[39mgaierror \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m    206\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m NameResolutionError(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhost, \u001b[38;5;28mself\u001b[39m, e) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01me\u001b[39;00m\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\site-packages\\urllib3\\util\\connection.py:81\u001b[0m, in \u001b[0;36mcreate_connection\u001b[1;34m(address, timeout, source_address, socket_options)\u001b[0m\n\u001b[0;32m     79\u001b[0m         err \u001b[38;5;241m=\u001b[39m _\n\u001b[0;32m     80\u001b[0m         \u001b[38;5;28;01mif\u001b[39;00m sock \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m---> 81\u001b[0m             \u001b[43msock\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mclose\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     83\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m err \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m     84\u001b[0m     \u001b[38;5;28;01mtry\u001b[39;00m:\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\socket.py:501\u001b[0m, in \u001b[0;36msocket.close\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    497\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_real_close\u001b[39m(\u001b[38;5;28mself\u001b[39m, _ss\u001b[38;5;241m=\u001b[39m_socket\u001b[38;5;241m.\u001b[39msocket):\n\u001b[0;32m    498\u001b[0m     \u001b[38;5;66;03m# This function should not reference any globals. See issue #808164.\u001b[39;00m\n\u001b[0;32m    499\u001b[0m     _ss\u001b[38;5;241m.\u001b[39mclose(\u001b[38;5;28mself\u001b[39m)\n\u001b[1;32m--> 501\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mclose\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[0;32m    502\u001b[0m     \u001b[38;5;66;03m# This function should not reference any globals. See issue #808164.\u001b[39;00m\n\u001b[0;32m    503\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_closed \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m    504\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_io_refs \u001b[38;5;241m<\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m:\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "import json\n",
    "from pathlib import Path\n",
    "import os\n",
    "import time\n",
    "\n",
    "class AITester:\n",
    "    def __init__(self):\n",
    "        self.base_url = \"http://localhost:8000/AI_ALL/\"\n",
    "        self.session = requests.Session()\n",
    "        \n",
    "        # 测试资源URL\n",
    "        self.test_urls = {\n",
    "            'image': 'http://obs.roseyy.cn/test.jpg',\n",
    "            'audio': 'https://obs.roseyy.cn/test.wav',\n",
    "            'video': 'https://obs.roseyy.cn/test.mp4'\n",
    "        }\n",
    "        \n",
    "        # 本地测试文件路径\n",
    "        self.local_files = {\n",
    "            'image': r'C:\\Users\\58300\\Desktop\\rag\\test.jpg',\n",
    "            'audio': r'C:\\Users\\58300\\Desktop\\rag\\test.wav',\n",
    "            'video': r'C:\\Users\\58300\\Desktop\\rag\\test.mp4'\n",
    "        }\n",
    "        \n",
    "        # 测试结果统计\n",
    "        self.test_results = {\n",
    "            'success': [],\n",
    "            'failed': []\n",
    "        }\n",
    "        \n",
    "        # 创建测试文件\n",
    "        self.init_test_files()\n",
    "    \n",
    "    def init_test_files(self):\n",
    "        \"\"\"初始化测试文件\"\"\"\n",
    "        test_dir = Path(\"test_files\")\n",
    "        test_dir.mkdir(exist_ok=True)\n",
    "        \n",
    "        # 创建测试文本文件\n",
    "        test_txt = test_dir / \"test.txt\"\n",
    "        if not test_txt.exists():\n",
    "            with open(test_txt, 'w', encoding='utf-8') as f:\n",
    "                f.write(\"这是一个测试文档。\\n用于测试长文本理解功能。\\n包含多行文本内容。\")\n",
    "            print(f\"创建测试文本: {test_txt}\")\n",
    "\n",
    "    def test_model(self, model_name, test_type, content, file_path=None, url=None):\n",
    "        \"\"\"通用模型测试方法\"\"\"\n",
    "        test_id = f\"{model_name}-{test_type}\"\n",
    "        data = {\n",
    "            'model': model_name,\n",
    "            'text': content\n",
    "        }\n",
    "        \n",
    "        if url:\n",
    "            data['url'] = url\n",
    "            \n",
    "        files = None\n",
    "        if file_path:\n",
    "            try:\n",
    "                files = {'file': open(file_path, 'rb')}\n",
    "            except Exception as e:\n",
    "                print(f\"打开文件失败: {e}\")\n",
    "                self.test_results['failed'].append((test_id, str(e)))\n",
    "                return\n",
    "        \n",
    "        print(f\"\\n测试 {model_name} {test_type}模型...\")\n",
    "        try:\n",
    "            response = self.session.post(self.base_url, data=data, files=files, stream=True)\n",
    "            self._process_response(response)\n",
    "            self.test_results['success'].append(test_id)\n",
    "        except Exception as e:\n",
    "            print(f\"请求错误: {e}\")\n",
    "            self.test_results['failed'].append((test_id, str(e)))\n",
    "        finally:\n",
    "            if files:\n",
    "                files['file'].close()\n",
    "\n",
    "    def _process_response(self, response):\n",
    "        \"\"\"处理流式响应\"\"\"\n",
    "        if response.status_code == 200:\n",
    "            for line in response.iter_lines():\n",
    "                if line:\n",
    "                    content = line.decode('utf-8')\n",
    "                    type_data = content.split(':', 1)\n",
    "                    if len(type_data) == 2:\n",
    "                        content_type, data = type_data\n",
    "                        print(f\"{content_type}: {data}\")\n",
    "        else:\n",
    "            print(f\"错误: {response.status_code}\")\n",
    "            print(response.text)\n",
    "\n",
    "    def test_cogview(self, prompt, size=\"1024x1024\"):\n",
    "        \"\"\"测试文生图功能\"\"\"\n",
    "        test_id = \"cogview-4-文生图\"\n",
    "        endpoint = \"http://localhost:8000/GLM-Cog/\"\n",
    "        \n",
    "        payload = {\n",
    "            \"model\": \"cogview-4\",\n",
    "            \"prompt\": prompt,\n",
    "            \"size\": size\n",
    "        }\n",
    "        \n",
    "        print(f\"\\n=== 测试 GLM-Cog ===\")\n",
    "        try:\n",
    "            response = self.session.post(endpoint, json=payload, headers={\"Content-Type\": \"application/json\"})\n",
    "            result = response.json()\n",
    "            \n",
    "            if response.status_code == 200:\n",
    "                print(\"生成的图片URL:\")\n",
    "                for idx, image in enumerate(result.get('data', [])):\n",
    "                    print(f\"图片 {idx + 1}: {image.get('url', '无URL')}\")\n",
    "                self.test_results['success'].append(test_id)\n",
    "            else:\n",
    "                error = result.get('error', '未知错误')\n",
    "                print(f\"错误: {error}\")\n",
    "                self.test_results['failed'].append((test_id, error))\n",
    "        except Exception as e:\n",
    "            print(f\"请求失败: {e}\")\n",
    "            self.test_results['failed'].append((test_id, str(e)))\n",
    "\n",
    "    def test_cogvideo(self, prompt, image_path=None):\n",
    "        \"\"\"测试视频生成功能\"\"\"\n",
    "        test_id = f\"cogvideox-{'图生' if image_path else '文生'}视频\"\n",
    "        endpoint = \"http://localhost:8000/GLM-CogVideo/\"\n",
    "        \n",
    "        payload = {\n",
    "            \"model\": \"cogvideox\",\n",
    "            \"prompt\": prompt,\n",
    "            \"quality\": \"quality\",\n",
    "            \"with_audio\": True,\n",
    "            \"size\": \"720x480\",\n",
    "            \"fps\": 30\n",
    "        }\n",
    "\n",
    "        if image_path:\n",
    "            try:\n",
    "                with open(image_path, 'rb') as f:\n",
    "                    image_data = base64.b64encode(f.read()).decode('utf-8')\n",
    "                    payload[\"image_url\"] = f\"data:image/jpeg;base64,{image_data}\"\n",
    "            except Exception as e:\n",
    "                print(f\"处理图片失败: {e}\")\n",
    "                self.test_results['failed'].append((test_id, str(e)))\n",
    "                return\n",
    "\n",
    "        print(f\"\\n=== 测试 {test_id} ===\")\n",
    "        try:\n",
    "            response = self.session.post(endpoint, json=payload, headers={\"Content-Type\": \"application/json\"})\n",
    "            if response.status_code != 200:\n",
    "                error = response.json().get('error', '未知错误')\n",
    "                print(f\"生成请求失败: {error}\")\n",
    "                self.test_results['failed'].append((test_id, error))\n",
    "                return\n",
    "\n",
    "            task_id = response.json().get('task_id')\n",
    "            if not task_id:\n",
    "                print(\"未获取到任务ID\")\n",
    "                self.test_results['failed'].append((test_id, \"未获取到任务ID\"))\n",
    "                return\n",
    "\n",
    "            print(f\"获取到任务ID: {task_id}\")\n",
    "            result = self.check_video_result(task_id, endpoint)\n",
    "            \n",
    "            if result:\n",
    "                print(f\"视频URL: {result.get('video_url')}\")\n",
    "                print(f\"封面URL: {result.get('cover_url')}\")\n",
    "                self.test_results['success'].append(test_id)\n",
    "            else:\n",
    "                self.test_results['failed'].append((test_id, \"生成失败\"))\n",
    "                \n",
    "        except Exception as e:\n",
    "            print(f\"请求失败: {e}\")\n",
    "            self.test_results['failed'].append((test_id, str(e)))\n",
    "\n",
    "    def check_video_result(self, task_id, endpoint, interval=20, max_retries=120):\n",
    "        \"\"\"检查视频生成结果\"\"\"\n",
    "        for _ in range(max_retries):\n",
    "            try:\n",
    "                check_payload = {\n",
    "                    \"action\": \"check_status\",\n",
    "                    \"task_id\": task_id\n",
    "                }\n",
    "                \n",
    "                response = self.session.post(endpoint, json=check_payload, headers={\"Content-Type\": \"application/json\"})\n",
    "                if response.status_code != 200:\n",
    "                    print(f\"检查任务状态失败: {response.json().get('error', '未知错误')}\")\n",
    "                    continue\n",
    "                    \n",
    "                result = response.json()\n",
    "                status = result.get('task_status')\n",
    "                print(f\"当前状态: {status}\")\n",
    "                \n",
    "                if status == \"SUCCESS\":\n",
    "                    return {\n",
    "                        \"video_url\": result.get('video_result', [{}])[0].get('url'),\n",
    "                        \"cover_url\": result.get('video_result', [{}])[0].get('cover_image_url')\n",
    "                    }\n",
    "                elif status in [\"FAIL\", \"failed\", \"error\"]:\n",
    "                    return None\n",
    "                    \n",
    "                time.sleep(interval)\n",
    "                \n",
    "            except Exception as e:\n",
    "                print(f\"检查结果失败: {e}\")\n",
    "                time.sleep(interval)\n",
    "        \n",
    "        print(\"等待超时\")\n",
    "        return None\n",
    "\n",
    "    def test_long_text(self, file_paths, question):\n",
    "        \"\"\"测试长文本理解\"\"\"\n",
    "        test_id = \"qwen-long-长文理解\"\n",
    "        print(f\"\\n=== 测试 {test_id} ===\")\n",
    "        \n",
    "        try:\n",
    "            # 读取并合并所有文件内容\n",
    "            combined_text = \"\"\n",
    "            for file_path in file_paths:\n",
    "                with open(file_path, 'r', encoding='utf-8') as f:\n",
    "                    combined_text += f.read() + \"\\n\\n\"\n",
    "            \n",
    "            # 修改消息格式\n",
    "            messages = [{\n",
    "                \"role\": \"user\",\n",
    "                \"content\": question + \"\\n\\n\" + combined_text\n",
    "            }]\n",
    "            \n",
    "            data = {\n",
    "                'model': 'qwen-long',\n",
    "                'messages': json.dumps(messages)\n",
    "            }\n",
    "            \n",
    "            response = self.session.post(self.base_url, data=data, stream=True)\n",
    "            if response.status_code == 200:\n",
    "                print(\"模型回复：\")\n",
    "                for line in response.iter_lines(decode_unicode=True):\n",
    "                    if line and line.startswith('text:'):\n",
    "                        print(line[5:])\n",
    "                self.test_results['success'].append(test_id)\n",
    "            else:\n",
    "                error = response.text\n",
    "                print(f\"错误: {error}\")\n",
    "                self.test_results['failed'].append((test_id, error))\n",
    "                \n",
    "        except Exception as e:\n",
    "            print(f\"测试失败: {e}\")\n",
    "            self.test_results['failed'].append((test_id, str(e)))\n",
    "\n",
    "    def print_test_results(self):\n",
    "        \"\"\"打印测试结果统计\"\"\"\n",
    "        print(\"\\n=== 测试结果统计 ===\")\n",
    "        print(f\"总计测试: {len(self.test_results['success']) + len(self.test_results['failed'])} 项\")\n",
    "        \n",
    "        if self.test_results['success']:\n",
    "            print(f\"\\n成功: {len(self.test_results['success'])} 项\")\n",
    "            print(\"成功的测试:\")\n",
    "            for test_id in self.test_results['success']:\n",
    "                print(f\"  - {test_id}\")\n",
    "        \n",
    "        if self.test_results['failed']:\n",
    "            print(f\"\\n失败: {len(self.test_results['failed'])} 项\")\n",
    "            print(\"失败的测试:\")\n",
    "            for test_id, error in self.test_results['failed']:\n",
    "                print(f\"  - {test_id}: {error}\")\n",
    "\n",
    "def main():\n",
    "    tester = AITester()\n",
    "    \n",
    "    try:\n",
    "        # 测试GLM-4\n",
    "        print(\"\\n=== 测试 GLM-4 ===\")\n",
    "        tester.test_model('glm-4', '对话',\n",
    "                         \"你好，请介绍一下你自己\")\n",
    "\n",
    "        # 测试GLM-4V\n",
    "        print(\"\\n=== 测试 GLM-4V ===\")\n",
    "        tester.test_model('glm-4v-plus', '图像URL',\n",
    "                         \"这张图片是什么内容？\",\n",
    "                         url=tester.test_urls['image'])\n",
    "        tester.test_model('glm-4v-plus', '图像文件',\n",
    "                         \"描述这张图片\",\n",
    "                         file_path=tester.local_files['image'])\n",
    "\n",
    "        # 测试文生图\n",
    "        tester.test_cogview(\"一只可爱的小猫咪\")\n",
    "        \n",
    "        # 测试视频生成\n",
    "        tester.test_cogvideo(\"一朵盛开的红玫瑰\")  # 文生视频\n",
    "        tester.test_cogvideo(\n",
    "            \"让这张图片动起来\",\n",
    "            r\"C:\\Users\\58300\\Desktop\\rag\\test.jpg\"  # 图生视频\n",
    "        )\n",
    "        \n",
    "        # 测试GLM-4-Voice\n",
    "        print(\"\\n=== 测试 GLM-4-Voice ===\")\n",
    "        tester.test_model('glm-4-voice', '语音',\n",
    "                         \"你好，请把这段话转成语音\")\n",
    "        tester.test_model('glm-4-voice', '语音识别',\n",
    "                         \"请识别这段音频\",\n",
    "                         file_path=tester.local_files['audio'])\n",
    "\n",
    "        # 测试QwenChat\n",
    "        print(\"\\n=== 测试 QwenChat ===\")\n",
    "        tester.test_model('qwen2.5-1.5b-instruct', '对话',\n",
    "                         \"你好，请介绍一下你自己\")\n",
    "\n",
    "        # 测试QwenOCR\n",
    "        print(\"\\n=== 测试 QwenOCR ===\")\n",
    "        tester.test_model('qwen-vl-ocr', 'OCR',\n",
    "                         \"识别图片中的文字\",\n",
    "                         file_path=tester.local_files['image'])\n",
    "\n",
    "        # 测试Qwenomni\n",
    "        print(\"\\n=== 测试 Qwenomni ===\")\n",
    "        tester.test_model('qwen-omni-turbo', '多模态',\n",
    "                         \"这张图片是什么内容？\",\n",
    "                         file_path=tester.local_files['image'])\n",
    "\n",
    "        # 测试QwenAudio\n",
    "        print(\"\\n=== 测试 QwenAudio ===\")\n",
    "        tester.test_model('qwen-audio-turbo-latest', '音频理解',\n",
    "                         \"这段音频说了什么？\",\n",
    "                         file_path=tester.local_files['audio'])\n",
    "        \n",
    "        # 测试长文本理解\n",
    "        md_files = [\n",
    "            r\"C:\\Users\\58300\\Desktop\\rag\\1.md\",\n",
    "            r\"C:\\Users\\58300\\Desktop\\rag\\2.md\",\n",
    "            r\"C:\\Users\\58300\\Desktop\\rag\\3.md\"\n",
    "        ]\n",
    "        tester.test_long_text(md_files, \"1.md的标题是，2.md的标题是，3.md的表态是\")\n",
    "        \n",
    "    finally:\n",
    "        tester.print_test_results()\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# glm接口方法\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'zhipuai'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[7], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mzhipuai\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m ZhipuAI\n\u001b[0;32m      2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mos\u001b[39;00m\n\u001b[0;32m      3\u001b[0m client \u001b[38;5;241m=\u001b[39m ZhipuAI(api_key\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m98fd7efde5b04a9ab8ded261b0cd54e5.iSTkJXlCzxR1rMC9\u001b[39m\u001b[38;5;124m\"\u001b[39m) \n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'zhipuai'"
     ]
    }
   ],
   "source": [
    "from zhipuai import ZhipuAI\n",
    "import os\n",
    "client = ZhipuAI(api_key=\"98fd7efde5b04a9ab8ded261b0cd54e5.iSTkJXlCzxR1rMC9\") \n",
    "response_text = \"\"  # 用于存储合并后的结果\n",
    "\n",
    "response = client.chat.completions.create(\n",
    "    model=\"glm-4-flash\",\n",
    "    messages=[\n",
    "        {\"role\": \"user\", \"content\": \"你是谁？\"},\n",
    "    ],\n",
    ")\n",
    "\n",
    "# 打印合并后的完整文本\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# glm 使用url方式\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'client' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[8], line 9\u001b[0m\n\u001b[0;32m      6\u001b[0m url \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhttps://open.bigmodel.cn/api/paas/v4/chat/completions\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m      8\u001b[0m \u001b[38;5;66;03m# 设置请求头和请求体\u001b[39;00m\n\u001b[1;32m----> 9\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[43mclient\u001b[49m\u001b[38;5;241m.\u001b[39mchat\u001b[38;5;241m.\u001b[39mcompletions\u001b[38;5;241m.\u001b[39mcreate(\n\u001b[0;32m     10\u001b[0m     model\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mglm-4-flash\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m     11\u001b[0m     messages\u001b[38;5;241m=\u001b[39m[\n\u001b[0;32m     12\u001b[0m         {\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrole\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124muser\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcontent\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m我得了抑郁症，我该怎么办？\u001b[39m\u001b[38;5;124m\"\u001b[39m},\n\u001b[0;32m     13\u001b[0m     ],\n\u001b[0;32m     14\u001b[0m     tools\u001b[38;5;241m=\u001b[39m[\n\u001b[0;32m     15\u001b[0m         {\n\u001b[0;32m     16\u001b[0m             \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtype\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mretrieval\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m     17\u001b[0m             \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mretrieval\u001b[39m\u001b[38;5;124m\"\u001b[39m: {\n\u001b[0;32m     18\u001b[0m                 \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mknowledge_id\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m1880239333915635712\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m     19\u001b[0m                 \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mprompt_template\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m从文档\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\\"\u001b[39;00m\u001b[38;5;130;01m\\\"\u001b[39;00m\u001b[38;5;130;01m\\\"\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m{{\u001b[39m\u001b[38;5;124mknowledge}}\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\\"\u001b[39;00m\u001b[38;5;130;01m\\\"\u001b[39;00m\u001b[38;5;130;01m\\\"\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m中找问题\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\\"\u001b[39;00m\u001b[38;5;130;01m\\\"\u001b[39;00m\u001b[38;5;130;01m\\\"\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m{{\u001b[39m\u001b[38;5;124mquestion}}\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\\"\u001b[39;00m\u001b[38;5;130;01m\\\"\u001b[39;00m\u001b[38;5;130;01m\\\"\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m的答案，找到答案就仅使用文档语句回答问题，找不到答案就用自身知识回答并且告诉用户该信息不是来自文档。\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m不要复述问题，直接开始回答。\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m     20\u001b[0m             }\n\u001b[0;32m     21\u001b[0m         }\n\u001b[0;32m     22\u001b[0m     ],\n\u001b[0;32m     23\u001b[0m     stream\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m,\n\u001b[0;32m     24\u001b[0m )\n\u001b[0;32m     26\u001b[0m \u001b[38;5;66;03m# 遍历响应中的每个 chunk，累加 content 到 response_text\u001b[39;00m\n\u001b[0;32m     27\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m chunk \u001b[38;5;129;01min\u001b[39;00m response:\n",
      "\u001b[1;31mNameError\u001b[0m: name 'client' is not defined"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "# 配置 API 密钥和 URL\n",
    "api_key = \"98fd7efde5b04a9ab8ded261b0cd54e5.iSTkJXlCzxR1rMC9\"\n",
    "url = \"https://open.bigmodel.cn/api/paas/v4/chat/completions\"\n",
    "\n",
    "# 设置请求头和请求体\n",
    "response = client.chat.completions.create(\n",
    "    model=\"glm-4-flash\",\n",
    "    messages=[\n",
    "        {\"role\": \"user\", \"content\": \"我得了抑郁症，我该怎么办？\"},\n",
    "    ],\n",
    "    tools=[\n",
    "        {\n",
    "            \"type\": \"retrieval\",\n",
    "            \"retrieval\": {\n",
    "                \"knowledge_id\": \"1880239333915635712\",\n",
    "                \"prompt_template\": \"从文档\\n\\\"\\\"\\\"\\n{{knowledge}}\\n\\\"\\\"\\\"\\n中找问题\\n\\\"\\\"\\\"\\n{{question}}\\n\\\"\\\"\\\"\\n的答案，找到答案就仅使用文档语句回答问题，找不到答案就用自身知识回答并且告诉用户该信息不是来自文档。\\n不要复述问题，直接开始回答。\"\n",
    "            }\n",
    "        }\n",
    "    ],\n",
    "    stream=True,\n",
    ")\n",
    "\n",
    "# 遍历响应中的每个 chunk，累加 content 到 response_text\n",
    "for chunk in response:\n",
    "    response_text += chunk.choices[0].delta.content\n",
    "\n",
    "# 打印合并后的完整文本\n",
    "print(response_text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 测试GLM-4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'choices': [{'finish_reason': 'stop', 'index': 0, 'message': {'content': '我是一个人工智能助手，名为 ChatGLM，是基于清华大学 KEG 实验室和智谱 AI 公司于 2024 年共同训练的语言模型开发的。我的任务是针对用户的问题和要求提供适当的答复和支持。', 'role': 'assistant'}}], 'created': 1739764414, 'id': '20250217115332f69ffded3f984280', 'model': 'glm-4-flash', 'request_id': '20250217115332f69ffded3f984280', 'usage': {'completion_tokens': 46, 'prompt_tokens': 7, 'total_tokens': 53}}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    " \n",
    "API_URL = \"http://localhost:8000/GLM-4/\"\n",
    "HEADERS = {\"Content-Type\": \"application/json\"}\n",
    " \n",
    "def simple_test(question, model=\"glm-4-flash\"):\n",
    "    payload = {\n",
    "        \"question\": question,\n",
    "        \"model\": model \n",
    "    }\n",
    "    try:\n",
    "        response = requests.post(API_URL,  json=payload, headers=HEADERS)\n",
    "        return response.json()   # 直接返回原始 JSON 数据\n",
    "    except Exception as e:\n",
    "        return {\"error\": f\"请求失败: {str(e)}\"}  # 返回错误信息的 JSON 格式\n",
    " \n",
    "if __name__ == \"__main__\":\n",
    "    # 简单测试\n",
    "    result = simple_test(\"你是谁\")\n",
    "    print(result)  # 打印返回的 JSON 数据\n",
    "    \n",
    "    # 测试其他模型（如需）\n",
    "    # result = simple_test(\"描述这张图片内容\", model=\"glm-4v\")\n",
    "    # print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 测试GLM-4V\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import base64\n",
    "import os\n",
    "\n",
    "API_URL = \"http://localhost:8000/GLM-4V/\"\n",
    "HEADERS = {\"Content-Type\": \"application/json\"}\n",
    "\n",
    "def test_glm4v():\n",
    "    # 固定测试内容\n",
    "    test_data = {\n",
    "        \"text\": \"这是一段测试文本\",\n",
    "        \"image_url\": \"http://obs.roseyy.cn/test.jpg\",\n",
    "        \"video_url\": \"https://obs.roseyy.cn/test.mp4\",\n",
    "        \"image_base64_path\": \"C:/Users/58300/Desktop/rag/test.jpg\",\n",
    "        \"video_base64_path\": \"C:/Users/58300/Desktop/rag/test.mp4\"\n",
    "    }\n",
    "\n",
    "    # 构建消息内容\n",
    "    content = []\n",
    "    \n",
    "    # 场景1: 测试图片URL-支持glm-4v-plus-0111、glm-4v-plus 、glm-4v、glm-4v-flash\n",
    "    content.append({\n",
    "        \"type\": \"image_url\",\n",
    "        \"image_url\": {\n",
    "            \"url\": test_data[\"image_url\"]\n",
    "        }\n",
    "    })\n",
    "    content.append({\n",
    "        \"type\": \"text\",\n",
    "        \"text\": \"请描述这张图片\"\n",
    "    })\n",
    "\n",
    "    # 或者场景2: 测试视频URL-支持glm-4v\n",
    "    # content.append({\n",
    "    #     \"type\": \"video_url\",\n",
    "    #     \"video_url\": {\n",
    "    #         \"url\": test_data[\"video_url\"]\n",
    "    #     }\n",
    "    # })\n",
    "    # content.append({\n",
    "    #     \"type\": \"text\",\n",
    "    #     \"text\": \"请描述这个视频的内容\"\n",
    "    # })\n",
    "\n",
    "    # 或者场景3: 测试Base64图片-支持glm-4v-plus-0111、glm-4v-plus 、glm-4v、glm-4v-flash\n",
    "    # with open(test_data[\"image_base64_path\"], \"rb\") as f:\n",
    "    #     img_base64 = base64.b64encode(f.read()).decode('utf-8')\n",
    "    # content.append({\n",
    "    #     \"type\": \"image_url\",\n",
    "    #     \"image_url\": {\n",
    "    #         \"url\": f\"data:image/jpeg;base64,{img_base64}\"\n",
    "    #     }\n",
    "    # })\n",
    "    # content.append({\n",
    "    #     \"type\": \"text\",\n",
    "    #     \"text\": \"分析这张图片\"\n",
    "    # })\n",
    "\n",
    "\n",
    "    # # 或者场景4: 测试Base64视频-支持glm-4v-plus-0111、glm-4v-plus 、glm-4v、glm-4v-flash\n",
    "    # with open(test_data[\"video_base64_path\"], \"rb\") as f:\n",
    "    #     video_base64 = base64.b64encode(f.read()).decode('utf-8')\n",
    "    # content.append({\n",
    "    #     \"type\": \"video_url\",\n",
    "    #     \"video_url\": {\n",
    "    #         \"url\": f\"data:video/mp4;base64,{video_base64}\"\n",
    "    #     }\n",
    "    # })\n",
    "    # content.append({\n",
    "    #     \"type\": \"text\",\n",
    "    #     \"text\": \"请描述这个视频\"\n",
    "    # })\n",
    "\n",
    "    payload = {\n",
    "        \"model\": \"glm-4v-flash\",  # 随时替换模型\n",
    "        \"messages\": [{\n",
    "            \"role\": \"user\",\n",
    "            \"content\": content\n",
    "        }]\n",
    "    }\n",
    "\n",
    "    try:\n",
    "        response = requests.post(API_URL, json=payload, headers=HEADERS)\n",
    "        result = response.json()\n",
    "        \n",
    "        print(\"\\n=== 测试结果 ===\")\n",
    "        print(f\"状态码: {response.status_code}\")\n",
    "        if response.status_code != 200:\n",
    "            print(f\"错误信息: {result.get('error', '未知错误')}\")\n",
    "        else:\n",
    "            print(\"响应内容:\")\n",
    "            print(result.get('choices', [{}])[0].get('message', {}).get('content', '无内容'))\n",
    "        \n",
    "    except Exception as e:\n",
    "        print(f\"请求失败: {str(e)}\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    test_glm4v()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 测试文生图glm-cogview"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "API_URL = \"http://localhost:8000/GLM-Cog/\"\n",
    "HEADERS = {\"Content-Type\": \"application/json\"}\n",
    "\n",
    "def test_cogview():\n",
    "    # 测试数据\n",
    "    test_cases = [\n",
    "        {\n",
    "            \"model\": \"cogview-3-flash\",#可用模型cogview-4；cogview-3-flash\n",
    "            \"prompt\": \"一只可爱的小猫咪\",\n",
    "            \"size\": \"1024x1024\"\n",
    "        },\n",
    "        # 可以添加更多测试用例\n",
    "    ]\n",
    "    \n",
    "    for case in test_cases:\n",
    "        print(f\"\\n测试用例: {case['prompt']}\")\n",
    "        \n",
    "        payload = {\n",
    "            \"model\": case[\"model\"],\n",
    "            \"prompt\": case[\"prompt\"],\n",
    "            \"size\": case[\"size\"]\n",
    "        }\n",
    "        \n",
    "        try:\n",
    "            response = requests.post(API_URL, json=payload, headers=HEADERS)\n",
    "            result = response.json()\n",
    "            \n",
    "            print(\"\\n=== 测试结果 ===\")\n",
    "            print(f\"状态码: {response.status_code}\")\n",
    "            if response.status_code != 200:\n",
    "                print(f\"错误信息: {result.get('error', '未知错误')}\")\n",
    "            else:\n",
    "                print(\"生成的图片URL:\")\n",
    "                # 根据实际返回格式调整获取图片URL的方式\n",
    "                images = result.get('data', [])\n",
    "                for idx, image in enumerate(images):\n",
    "                    print(f\"图片 {idx + 1}: {image.get('url', '无URL')}\")\n",
    "            \n",
    "        except Exception as e:\n",
    "            print(f\"请求失败: {str(e)}\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    test_cogview() "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 文生视频GLM-cogvideox 测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "=== 测试用例3：本地图片生成 ===\n",
      "获取到任务ID: 36781736753330461-8960016563948002395\n",
      "检查任务状态失败: 'NoneType' object is not iterable\n",
      "检查任务状态失败: 'NoneType' object is not iterable\n",
      "检查任务状态失败: 'NoneType' object is not iterable\n",
      "检查任务状态失败: 'NoneType' object is not iterable\n",
      "检查任务状态失败: 'NoneType' object is not iterable\n",
      "检查任务状态失败: 'NoneType' object is not iterable\n",
      "检查任务状态失败: 'NoneType' object is not iterable\n",
      "检查任务状态失败: 'NoneType' object is not iterable\n",
      "检查任务状态失败: 'NoneType' object is not iterable\n",
      "检查任务状态失败: 'NoneType' object is not iterable\n",
      "检查任务状态失败: 'NoneType' object is not iterable\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[5], line 125\u001b[0m\n\u001b[0;32m    105\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m__main__\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[0;32m    106\u001b[0m     \u001b[38;5;66;03m# # 测试用例1：仅文本生成\u001b[39;00m\n\u001b[0;32m    107\u001b[0m     \u001b[38;5;66;03m# print(\"\\n=== 测试用例1：仅文本生成 ===\")\u001b[39;00m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    122\u001b[0m     \n\u001b[0;32m    123\u001b[0m     \u001b[38;5;66;03m# 测试用例3：本地图片生成\u001b[39;00m\n\u001b[0;32m    124\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m=== 测试用例3：本地图片生成 ===\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m--> 125\u001b[0m     result3 \u001b[38;5;241m=\u001b[39m \u001b[43mtest_generate_video\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    126\u001b[0m \u001b[43m        \u001b[49m\u001b[43mprompt\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m让图片动起来\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m    127\u001b[0m \u001b[43m        \u001b[49m\u001b[43mimage_input\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mC:/Users/58300/Desktop/rag/test.jpg\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m    128\u001b[0m \u001b[43m        \u001b[49m\u001b[43mmodel\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcogvideox-flash\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\n\u001b[0;32m    129\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    130\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m生成结果:\u001b[39m\u001b[38;5;124m\"\u001b[39m, result3) \n",
      "Cell \u001b[1;32mIn[5], line 56\u001b[0m, in \u001b[0;36mtest_generate_video\u001b[1;34m(prompt, image_input, model)\u001b[0m\n\u001b[0;32m     53\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m获取到任务ID: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mtask_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m     55\u001b[0m     \u001b[38;5;66;03m# 轮询检查结果\u001b[39;00m\n\u001b[1;32m---> 56\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mcheck_video_result\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtask_id\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     58\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m     59\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m请求失败: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00me\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
      "Cell \u001b[1;32mIn[5], line 72\u001b[0m, in \u001b[0;36mcheck_video_result\u001b[1;34m(task_id, interval, max_retries)\u001b[0m\n\u001b[0;32m     65\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m     66\u001b[0m     \u001b[38;5;66;03m# 构建状态查询请求\u001b[39;00m\n\u001b[0;32m     67\u001b[0m     check_payload \u001b[38;5;241m=\u001b[39m {\n\u001b[0;32m     68\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124maction\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcheck_status\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m     69\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtask_id\u001b[39m\u001b[38;5;124m\"\u001b[39m: task_id\n\u001b[0;32m     70\u001b[0m     }\n\u001b[1;32m---> 72\u001b[0m     response \u001b[38;5;241m=\u001b[39m \u001b[43mrequests\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpost\u001b[49m\u001b[43m(\u001b[49m\u001b[43mAPI_URL\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mjson\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcheck_payload\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mheaders\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mHEADERS\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     73\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m response\u001b[38;5;241m.\u001b[39mstatus_code \u001b[38;5;241m!=\u001b[39m \u001b[38;5;241m200\u001b[39m:\n\u001b[0;32m     74\u001b[0m         \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m检查任务状态失败: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mresponse\u001b[38;5;241m.\u001b[39mjson()\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124merror\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;250m \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m未知错误\u001b[39m\u001b[38;5;124m'\u001b[39m)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\site-packages\\requests\\api.py:115\u001b[0m, in \u001b[0;36mpost\u001b[1;34m(url, data, json, **kwargs)\u001b[0m\n\u001b[0;32m    103\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mpost\u001b[39m(url, data\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, json\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[0;32m    104\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124;03m\"\"\"Sends a POST request.\u001b[39;00m\n\u001b[0;32m    105\u001b[0m \n\u001b[0;32m    106\u001b[0m \u001b[38;5;124;03m    :param url: URL for the new :class:`Request` object.\u001b[39;00m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    112\u001b[0m \u001b[38;5;124;03m    :rtype: requests.Response\u001b[39;00m\n\u001b[0;32m    113\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n\u001b[1;32m--> 115\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mrequest\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mpost\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdata\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mjson\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mjson\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\site-packages\\requests\\api.py:59\u001b[0m, in \u001b[0;36mrequest\u001b[1;34m(method, url, **kwargs)\u001b[0m\n\u001b[0;32m     55\u001b[0m \u001b[38;5;66;03m# By using the 'with' statement we are sure the session is closed, thus we\u001b[39;00m\n\u001b[0;32m     56\u001b[0m \u001b[38;5;66;03m# avoid leaving sockets open which can trigger a ResourceWarning in some\u001b[39;00m\n\u001b[0;32m     57\u001b[0m \u001b[38;5;66;03m# cases, and look like a memory leak in others.\u001b[39;00m\n\u001b[0;32m     58\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m sessions\u001b[38;5;241m.\u001b[39mSession() \u001b[38;5;28;01mas\u001b[39;00m session:\n\u001b[1;32m---> 59\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43msession\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmethod\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmethod\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43murl\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\site-packages\\requests\\sessions.py:589\u001b[0m, in \u001b[0;36mSession.request\u001b[1;34m(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)\u001b[0m\n\u001b[0;32m    584\u001b[0m send_kwargs \u001b[38;5;241m=\u001b[39m {\n\u001b[0;32m    585\u001b[0m     \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtimeout\u001b[39m\u001b[38;5;124m\"\u001b[39m: timeout,\n\u001b[0;32m    586\u001b[0m     \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mallow_redirects\u001b[39m\u001b[38;5;124m\"\u001b[39m: allow_redirects,\n\u001b[0;32m    587\u001b[0m }\n\u001b[0;32m    588\u001b[0m send_kwargs\u001b[38;5;241m.\u001b[39mupdate(settings)\n\u001b[1;32m--> 589\u001b[0m resp \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msend\u001b[49m\u001b[43m(\u001b[49m\u001b[43mprep\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43msend_kwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    591\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m resp\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\site-packages\\requests\\sessions.py:703\u001b[0m, in \u001b[0;36mSession.send\u001b[1;34m(self, request, **kwargs)\u001b[0m\n\u001b[0;32m    700\u001b[0m start \u001b[38;5;241m=\u001b[39m preferred_clock()\n\u001b[0;32m    702\u001b[0m \u001b[38;5;66;03m# Send the request\u001b[39;00m\n\u001b[1;32m--> 703\u001b[0m r \u001b[38;5;241m=\u001b[39m \u001b[43madapter\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msend\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    705\u001b[0m \u001b[38;5;66;03m# Total elapsed time of the request (approximately)\u001b[39;00m\n\u001b[0;32m    706\u001b[0m elapsed \u001b[38;5;241m=\u001b[39m preferred_clock() \u001b[38;5;241m-\u001b[39m start\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\site-packages\\requests\\adapters.py:667\u001b[0m, in \u001b[0;36mHTTPAdapter.send\u001b[1;34m(self, request, stream, timeout, verify, cert, proxies)\u001b[0m\n\u001b[0;32m    664\u001b[0m     timeout \u001b[38;5;241m=\u001b[39m TimeoutSauce(connect\u001b[38;5;241m=\u001b[39mtimeout, read\u001b[38;5;241m=\u001b[39mtimeout)\n\u001b[0;32m    666\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 667\u001b[0m     resp \u001b[38;5;241m=\u001b[39m \u001b[43mconn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43murlopen\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    668\u001b[0m \u001b[43m        \u001b[49m\u001b[43mmethod\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmethod\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    669\u001b[0m \u001b[43m        \u001b[49m\u001b[43murl\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    670\u001b[0m \u001b[43m        \u001b[49m\u001b[43mbody\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbody\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    671\u001b[0m \u001b[43m        \u001b[49m\u001b[43mheaders\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mheaders\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    672\u001b[0m \u001b[43m        \u001b[49m\u001b[43mredirect\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m    673\u001b[0m \u001b[43m        \u001b[49m\u001b[43massert_same_host\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m    674\u001b[0m \u001b[43m        \u001b[49m\u001b[43mpreload_content\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m    675\u001b[0m \u001b[43m        \u001b[49m\u001b[43mdecode_content\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m    676\u001b[0m \u001b[43m        \u001b[49m\u001b[43mretries\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmax_retries\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    677\u001b[0m \u001b[43m        \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtimeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    678\u001b[0m \u001b[43m        \u001b[49m\u001b[43mchunked\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mchunked\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    679\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    681\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m (ProtocolError, \u001b[38;5;167;01mOSError\u001b[39;00m) \u001b[38;5;28;01mas\u001b[39;00m err:\n\u001b[0;32m    682\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mConnectionError\u001b[39;00m(err, request\u001b[38;5;241m=\u001b[39mrequest)\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\site-packages\\urllib3\\connectionpool.py:789\u001b[0m, in \u001b[0;36mHTTPConnectionPool.urlopen\u001b[1;34m(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, preload_content, decode_content, **response_kw)\u001b[0m\n\u001b[0;32m    786\u001b[0m response_conn \u001b[38;5;241m=\u001b[39m conn \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m release_conn \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m    788\u001b[0m \u001b[38;5;66;03m# Make the request on the HTTPConnection object\u001b[39;00m\n\u001b[1;32m--> 789\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_make_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    790\u001b[0m \u001b[43m    \u001b[49m\u001b[43mconn\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    791\u001b[0m \u001b[43m    \u001b[49m\u001b[43mmethod\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    792\u001b[0m \u001b[43m    \u001b[49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    793\u001b[0m \u001b[43m    \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtimeout_obj\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    794\u001b[0m \u001b[43m    \u001b[49m\u001b[43mbody\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mbody\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    795\u001b[0m \u001b[43m    \u001b[49m\u001b[43mheaders\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mheaders\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    796\u001b[0m \u001b[43m    \u001b[49m\u001b[43mchunked\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mchunked\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    797\u001b[0m \u001b[43m    \u001b[49m\u001b[43mretries\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mretries\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    798\u001b[0m \u001b[43m    \u001b[49m\u001b[43mresponse_conn\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mresponse_conn\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    799\u001b[0m \u001b[43m    \u001b[49m\u001b[43mpreload_content\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpreload_content\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    800\u001b[0m \u001b[43m    \u001b[49m\u001b[43mdecode_content\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdecode_content\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    801\u001b[0m \u001b[43m    \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mresponse_kw\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    802\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    804\u001b[0m \u001b[38;5;66;03m# Everything went great!\u001b[39;00m\n\u001b[0;32m    805\u001b[0m clean_exit \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\site-packages\\urllib3\\connectionpool.py:536\u001b[0m, in \u001b[0;36mHTTPConnectionPool._make_request\u001b[1;34m(self, conn, method, url, body, headers, retries, timeout, chunked, response_conn, preload_content, decode_content, enforce_content_length)\u001b[0m\n\u001b[0;32m    534\u001b[0m \u001b[38;5;66;03m# Receive the response from the server\u001b[39;00m\n\u001b[0;32m    535\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 536\u001b[0m     response \u001b[38;5;241m=\u001b[39m \u001b[43mconn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgetresponse\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    537\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m (BaseSSLError, \u001b[38;5;167;01mOSError\u001b[39;00m) \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m    538\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_raise_timeout(err\u001b[38;5;241m=\u001b[39me, url\u001b[38;5;241m=\u001b[39murl, timeout_value\u001b[38;5;241m=\u001b[39mread_timeout)\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\site-packages\\urllib3\\connection.py:507\u001b[0m, in \u001b[0;36mHTTPConnection.getresponse\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    504\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mresponse\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m HTTPResponse\n\u001b[0;32m    506\u001b[0m \u001b[38;5;66;03m# Get the response from http.client.HTTPConnection\u001b[39;00m\n\u001b[1;32m--> 507\u001b[0m httplib_response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgetresponse\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    509\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m    510\u001b[0m     assert_header_parsing(httplib_response\u001b[38;5;241m.\u001b[39mmsg)\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\http\\client.py:1428\u001b[0m, in \u001b[0;36mHTTPConnection.getresponse\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m   1426\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m   1427\u001b[0m     \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m-> 1428\u001b[0m         \u001b[43mresponse\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbegin\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1429\u001b[0m     \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mConnectionError\u001b[39;00m:\n\u001b[0;32m   1430\u001b[0m         \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mclose()\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\http\\client.py:331\u001b[0m, in \u001b[0;36mHTTPResponse.begin\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    329\u001b[0m \u001b[38;5;66;03m# read until we get a non-100 response\u001b[39;00m\n\u001b[0;32m    330\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m--> 331\u001b[0m     version, status, reason \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_read_status\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    332\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m status \u001b[38;5;241m!=\u001b[39m CONTINUE:\n\u001b[0;32m    333\u001b[0m         \u001b[38;5;28;01mbreak\u001b[39;00m\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\http\\client.py:292\u001b[0m, in \u001b[0;36mHTTPResponse._read_status\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    291\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_read_status\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[1;32m--> 292\u001b[0m     line \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mstr\u001b[39m(\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreadline\u001b[49m\u001b[43m(\u001b[49m\u001b[43m_MAXLINE\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124miso-8859-1\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m    293\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(line) \u001b[38;5;241m>\u001b[39m _MAXLINE:\n\u001b[0;32m    294\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m LineTooLong(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mstatus line\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "File \u001b[1;32md:\\APP SetUP\\anaconda\\Lib\\socket.py:720\u001b[0m, in \u001b[0;36mSocketIO.readinto\u001b[1;34m(self, b)\u001b[0m\n\u001b[0;32m    718\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[0;32m    719\u001b[0m     \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 720\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_sock\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrecv_into\u001b[49m\u001b[43m(\u001b[49m\u001b[43mb\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    721\u001b[0m     \u001b[38;5;28;01mexcept\u001b[39;00m timeout:\n\u001b[0;32m    722\u001b[0m         \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_timeout_occurred \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "import base64\n",
    "import time\n",
    "import requests\n",
    "\n",
    "API_URL = \"http://localhost:8000/GLM-CogVideo/\"\n",
    "HEADERS = {\"Content-Type\": \"application/json\"}\n",
    "\n",
    "def test_generate_video(prompt, image_input=None, model=\"cogvideox-flash\"):\n",
    "    \"\"\"\n",
    "    测试视频生成\n",
    "    :param prompt: 文本描述\n",
    "    :param image_input: 图片输入（可选，支持URL或本地路径）\n",
    "    :param model: 模型名称\n",
    "    :return: 生成的视频URL\n",
    "    \"\"\"\n",
    "    # 处理图片输入\n",
    "    image_url = None\n",
    "    if image_input:\n",
    "        if image_input.startswith(('http://', 'https://')):\n",
    "            image_url = image_input\n",
    "        else:\n",
    "            # 处理本地文件\n",
    "            try:\n",
    "                with open(image_input, 'rb') as f:\n",
    "                    image_url = f\"data:image/jpeg;base64,{base64.b64encode(f.read()).decode()}\"\n",
    "            except Exception as e:\n",
    "                print(f\"处理图片失败: {e}\")\n",
    "                return None\n",
    "\n",
    "    # 构建请求数据\n",
    "    payload = {\n",
    "        \"model\": model,\n",
    "        \"prompt\": prompt,\n",
    "        \"image_url\": image_url,\n",
    "        \"quality\": \"quality\",\n",
    "        \"with_audio\": True,\n",
    "        \"size\": \"720x480\",\n",
    "        \"fps\": 30\n",
    "    }\n",
    "\n",
    "    try:\n",
    "        # 发起生成请求\n",
    "        response = requests.post(API_URL, json=payload, headers=HEADERS)\n",
    "        if response.status_code != 200:\n",
    "            print(f\"生成请求失败: {response.json().get('error', '未知错误')}\")\n",
    "            return None\n",
    "            \n",
    "        task_id = response.json().get('task_id')\n",
    "        if not task_id:\n",
    "            print(\"未获取到任务ID\")\n",
    "            return None\n",
    "            \n",
    "        print(f\"获取到任务ID: {task_id}\")\n",
    "        \n",
    "        # 轮询检查结果\n",
    "        return check_video_result(task_id)\n",
    "        \n",
    "    except Exception as e:\n",
    "        print(f\"请求失败: {e}\")\n",
    "        return None\n",
    "\n",
    "def check_video_result(task_id, interval=20, max_retries=120):\n",
    "    \"\"\"检查视频生成结果\"\"\"\n",
    "    for _ in range(max_retries):\n",
    "        try:\n",
    "            # 构建状态查询请求\n",
    "            check_payload = {\n",
    "                \"action\": \"check_status\",\n",
    "                \"task_id\": task_id\n",
    "            }\n",
    "            \n",
    "            response = requests.post(API_URL, json=check_payload, headers=HEADERS)\n",
    "            if response.status_code != 200:\n",
    "                print(f\"检查任务状态失败: {response.json().get('error', '未知错误')}\")\n",
    "                continue\n",
    "                \n",
    "            result = response.json()\n",
    "            status = result.get('task_status')\n",
    "            print(f\"当前状态: {status}\")\n",
    "            \n",
    "            if status == \"SUCCESS\":\n",
    "                video_result = result.get('video_result', [])\n",
    "                if not video_result:\n",
    "                    print(\"无视频结果\")\n",
    "                    return None\n",
    "                    \n",
    "                first_video = video_result[0]\n",
    "                return {\n",
    "                    \"video_url\": first_video.get('url'),\n",
    "                    \"cover_url\": first_video.get('cover_image_url')\n",
    "                }\n",
    "            elif status in [\"FAIL\", \"failed\", \"error\"]:\n",
    "                print(\"视频生成失败\")\n",
    "                return None\n",
    "                \n",
    "            time.sleep(interval)\n",
    "            \n",
    "        except Exception as e:\n",
    "            print(f\"检查结果失败: {e}\")\n",
    "            time.sleep(interval)\n",
    "    \n",
    "    print(\"等待超时\")\n",
    "    return None\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    # # 测试用例1：仅文本生成\n",
    "    # print(\"\\n=== 测试用例1：仅文本生成 ===\")\n",
    "    # result1 = test_generate_video(\n",
    "    #     prompt=\"红色玫瑰\",\n",
    "    #     model=\"cogvideox-flash\"\n",
    "    # )\n",
    "    # print(\"生成结果:\", result1)\n",
    "    \n",
    "    # # 测试用例2：图片URL生成\n",
    "    # print(\"\\n=== 测试用例2：图片URL生成 ===\")\n",
    "    # result2 = test_generate_video(\n",
    "    #     prompt=\"让这朵花绽放\",\n",
    "    #     image_input=\"http://obs.roseyy.cn/test.jpg\",\n",
    "    #     model=\"cogvideox-flash\"\n",
    "    # )\n",
    "    # print(\"生成结果:\", result2)\n",
    "    \n",
    "    # 测试用例3：本地图片生成\n",
    "    print(\"\\n=== 测试用例3：本地图片生成 ===\")\n",
    "    result3 = test_generate_video(\n",
    "        prompt=\"让图片动起来\",\n",
    "        image_input=\"C:/Users/58300/Desktop/rag/test.jpg\",\n",
    "        model=\"cogvideox-flash\"\n",
    "    )\n",
    "    print(\"生成结果:\", result3) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# GLM-4-VOICE智能对话测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import base64\n",
    "import wave\n",
    "import requests\n",
    "import time\n",
    "from zhipuai import ZhipuAI\n",
    "\n",
    "API_URL = \"http://localhost:8000/GLM-4-Voice/\"\n",
    "HEADERS = {\"Content-Type\": \"application/json\"}\n",
    "\n",
    "def check_server():\n",
    "    \"\"\"检查服务器是否运行\"\"\"\n",
    "    try:\n",
    "        # 改用我们已有的API端点来检查\n",
    "        response = requests.get(\"http://localhost:8000/GLM-4-Voice/\")\n",
    "        # 即使返回405（因为只支持POST），也说明服务器在运行\n",
    "        return response.status_code in [200, 404, 405]\n",
    "    except requests.exceptions.ConnectionError:\n",
    "        return False\n",
    "\n",
    "def wait_for_server(timeout=30):\n",
    "    \"\"\"等待服务器启动\"\"\"\n",
    "    start_time = time.time()\n",
    "    while time.time() - start_time < timeout:\n",
    "        if check_server():\n",
    "            print(\"服务器已就绪\")\n",
    "            return True\n",
    "        print(\"等待服务器启动...\")\n",
    "        time.sleep(2)\n",
    "    return False\n",
    "\n",
    "def save_audio_as_wav(audio_data, filepath):\n",
    "    \"\"\"\n",
    "    将音频数据保存为WAV文件\n",
    "    :param audio_data: 音频数据（字节流）\n",
    "    :param filepath: 保存路径\n",
    "    \"\"\"\n",
    "    with wave.open(filepath, 'wb') as wav_file:\n",
    "        wav_file.setnchannels(1)   # 单声道\n",
    "        wav_file.setsampwidth(2)   # 每个样本2字节\n",
    "        wav_file.setframerate(44100)   # 采样率44100 Hz\n",
    "        wav_file.writeframes(audio_data)\n",
    "    print(f\"音频已保存到 {filepath}\")\n",
    "\n",
    "def test_voice_generation(prompt=None, audio_path=None, model=\"glm-4-voice\"):\n",
    "    \"\"\"\n",
    "    测试语音生成\n",
    "    :param prompt: 文本提示\n",
    "    :param audio_path: 音频文件路径（可选）\n",
    "    :param model: 模型名称\n",
    "    :return: 生成的文本和音频\n",
    "    \"\"\"\n",
    "    # 检查服务器状态\n",
    "    if not wait_for_server():\n",
    "        print(\"无法连接到服务器，请确保Django服务已启动\")\n",
    "        return None\n",
    "\n",
    "    # 构建消息内容\n",
    "    content = []\n",
    "    \n",
    "    # 添加文本内容\n",
    "    if prompt:\n",
    "        content.append({\n",
    "            \"type\": \"text\",\n",
    "            \"text\": prompt\n",
    "        })\n",
    "    \n",
    "    # 处理音频输入\n",
    "    if audio_path:\n",
    "        try:\n",
    "            # 读取音频文件并转换为Base64编码\n",
    "            with open(audio_path, \"rb\") as audio_file:\n",
    "                audio_data = audio_file.read()\n",
    "                audio_base64 = base64.b64encode(audio_data).decode(\"utf-8\")\n",
    "            \n",
    "            # 获取音频格式\n",
    "            audio_format = \"wav\" if audio_path.lower().endswith(\".wav\") else \"mp3\"\n",
    "            \n",
    "            content.append({\n",
    "                \"type\": \"input_audio\",\n",
    "                \"input_audio\": {\n",
    "                    \"data\": audio_base64,\n",
    "                    \"format\": audio_format\n",
    "                }\n",
    "            })\n",
    "            \n",
    "        except Exception as e:\n",
    "            print(f\"处理音频文件失败: {e}\")\n",
    "            return None\n",
    "    \n",
    "    # 构建请求数据\n",
    "    payload = {\n",
    "        \"model\": model,\n",
    "        \"messages\": [{\n",
    "            \"role\": \"user\",\n",
    "            \"content\": content\n",
    "        }],\n",
    "        \"temperature\": 0.8,\n",
    "        \"top_p\": 0.6,\n",
    "        \"max_tokens\": 1024,\n",
    "        \"do_sample\": True,\n",
    "        \"stream\": False\n",
    "    }\n",
    "    \n",
    "    try:\n",
    "        # 发起请求\n",
    "        response = requests.post(API_URL, json=payload, headers=HEADERS)\n",
    "        if response.status_code != 200:\n",
    "            print(f\"请求失败: {response.json().get('error', '未知错误')}\")\n",
    "            return None\n",
    "        \n",
    "        result = response.json()\n",
    "        \n",
    "        # 如果有音频数据，保存为文件\n",
    "        if \"choices\" in result and result[\"choices\"]:\n",
    "            choice = result[\"choices\"][0]\n",
    "            if \"message\" in choice and \"audio\" in choice[\"message\"]:\n",
    "                try:\n",
    "                    audio_data = base64.b64decode(choice[\"message\"][\"audio\"][\"data\"])\n",
    "                    save_audio_as_wav(audio_data, \"output.wav\")\n",
    "                except Exception as e:\n",
    "                    print(f\"保存音频失败: {e}\")\n",
    "        \n",
    "        return result\n",
    "        \n",
    "    except Exception as e:\n",
    "        print(f\"请求失败: {e}\")\n",
    "        return None\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    # # 测试用例1：仅文本生成\n",
    "    # print(\"\\n=== 测试用例1：仅文本生成 ===\")\n",
    "    # result1 = test_voice_generation(\n",
    "    #     prompt=\"给我讲个笑话\",\n",
    "    #     model=\"glm-4-voice\"\n",
    "    # )\n",
    "    # print(\"生成结果:\", result1)\n",
    "    \n",
    "    # 测试用例2：文本和音频生成\n",
    "    print(\"\\n=== 测试用例2：文本和音频生成 ===\")\n",
    "    result2 = test_voice_generation(\n",
    "        prompt=\"这段话说得怎么样？\",\n",
    "        audio_path=\"D:/python代码/rvc/sound/limei/output2/limei.wav_0000000000_0000206080.wav\",\n",
    "        model=\"glm-4-voice\"\n",
    "    )\n",
    "    print(\"生成结果:\", result2) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# coze_chat模型体测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "对话内容: 我是豆包呀，能陪你解答各种问题和提供各种信息呢~ 有什么事都可以问我哦。\n",
      "Token用量: 1266\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "def test_coze_chat():\n",
    "    \"\"\"测试COZE对话\"\"\"\n",
    "    API_URL = \"http://localhost:8000/CozeChat/\"\n",
    "    HEADERS = {\"Content-Type\": \"application/json\"}\n",
    "    \n",
    "    # 测试数据 - 需要提供question和user_id\n",
    "    payload = {\n",
    "        \"question\": \"你是谁？\",\n",
    "        \"user_id\": \"123\"  # 用户标识必须提供\n",
    "        # api_token和bot_id有默认值，可以省略\n",
    "    }\n",
    "    \n",
    "    try:\n",
    "        # 发起请求\n",
    "        response = requests.post(API_URL, json=payload, headers=HEADERS)\n",
    "        if response.status_code != 200:\n",
    "            print(f\"请求失败: {response.json().get('error', '未知错误')}\")\n",
    "            return None\n",
    "            \n",
    "        # 处理响应\n",
    "        result = response.json()\n",
    "        print(\"对话内容:\", result.get(\"content\"))\n",
    "        print(\"Token用量:\", result.get(\"token_count\"))\n",
    "        return result\n",
    "        \n",
    "    except Exception as e:\n",
    "        print(f\"请求失败: {e}\")\n",
    "        return None\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    test_coze_chat() "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Qwen大语言模型测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "=== 通义千问测试程序 ===\n",
      "1. 单轮对话测试\n",
      "2. 长文本对话测试\n",
      "3. 多轮对话测试\n",
      "0. 退出程序\n",
      "\n",
      "助手: 你好！虽然我是你好！虽然我是由深度求索你好！虽然我是由深度求索 dzxl20你好！虽然我是由深度求索 dzxl2023 人工智能你好！虽然我是由深度求索 dzxl2023 人工智能助手 内容你好！虽然我是由深度求索 dzxl2023 人工智能助手 内容安全 设置起来的你好！虽然我是由深度求索 dzxl2023 人工智能助手 内容安全 设置起来的，但我完全愿意你好！虽然我是由深度求索 dzxl2023 人工智能助手 内容安全 设置起来的，但我完全愿意为你解决任何相关你好！虽然我是由深度求索 dzxl2023 人工智能助手 内容安全 设置起来的，但我完全愿意为你解决任何相关问题。如果你有任何你好！虽然我是由深度求索 dzxl2023 人工智能助手 内容安全 设置起来的，但我完全愿意为你解决任何相关问题。如果你有任何心理或心理学方面的你好！虽然我是由深度求索 dzxl2023 人工智能助手 内容安全 设置起来的，但我完全愿意为你解决任何相关问题。如果你有任何心理或心理学方面的疑问，我很乐意你好！虽然我是由深度求索 dzxl2023 人工智能助手 内容安全 设置起来的，但我完全愿意为你解决任何相关问题。如果你有任何心理或心理学方面的疑问，我很乐意为你解答！无论是你好！虽然我是由深度求索 dzxl2023 人工智能助手 内容安全 设置起来的，但我完全愿意为你解决任何相关问题。如果你有任何心理或心理学方面的疑问，我很乐意为你解答！无论是关于心理评估、你好！虽然我是由深度求索 dzxl2023 人工智能助手 内容安全 设置起来的，但我完全愿意为你解决任何相关问题。如果你有任何心理或心理学方面的疑问，我很乐意为你解答！无论是关于心理评估、人格测试、心理你好！虽然我是由深度求索 dzxl2023 人工智能助手 内容安全 设置起来的，但我完全愿意为你解决任何相关问题。如果你有任何心理或心理学方面的疑问，我很乐意为你解答！无论是关于心理评估、人格测试、心理再生研究，还是你好！虽然我是由深度求索 dzxl2023 人工智能助手 内容安全 设置起来的，但我完全愿意为你解决任何相关问题。如果你有任何心理或心理学方面的疑问，我很乐意为你解答！无论是关于心理评估、人格测试、心理再生研究，还是关于心理咨询相关的问题你好！虽然我是由深度求索 dzxl2023 人工智能助手 内容安全 设置起来的，但我完全愿意为你解决任何相关问题。如果你有任何心理或心理学方面的疑问，我很乐意为你解答！无论是关于心理评估、人格测试、心理再生研究，还是关于心理咨询相关的问题，我都会尽力你好！虽然我是由深度求索 dzxl2023 人工智能助手 内容安全 设置起来的，但我完全愿意为你解决任何相关问题。如果你有任何心理或心理学方面的疑问，我很乐意为你解答！无论是关于心理评估、人格测试、心理再生研究，还是关于心理咨询相关的问题，我都会尽力提供专业的帮助。你好！虽然我是由深度求索 dzxl2023 人工智能助手 内容安全 设置起来的，但我完全愿意为你解决任何相关问题。如果你有任何心理或心理学方面的疑问，我很乐意为你解答！无论是关于心理评估、人格测试、心理再生研究，还是关于心理咨询相关的问题，我都会尽力提供专业的帮助。请随时提出你的你好！虽然我是由深度求索 dzxl2023 人工智能助手 内容安全 设置起来的，但我完全愿意为你解决任何相关问题。如果你有任何心理或心理学方面的疑问，我很乐意为你解答！无论是关于心理评估、人格测试、心理再生研究，还是关于心理咨询相关的问题，我都会尽力提供专业的帮助。请随时提出你的问题，我会尽力你好！虽然我是由深度求索 dzxl2023 人工智能助手 内容安全 设置起来的，但我完全愿意为你解决任何相关问题。如果你有任何心理或心理学方面的疑问，我很乐意为你解答！无论是关于心理评估、人格测试、心理再生研究，还是关于心理咨询相关的问题，我都会尽力提供专业的帮助。请随时提出你的问题，我会尽力为你解答。期待你好！虽然我是由深度求索 dzxl2023 人工智能助手 内容安全 设置起来的，但我完全愿意为你解决任何相关问题。如果你有任何心理或心理学方面的疑问，我很乐意为你解答！无论是关于心理评估、人格测试、心理再生研究，还是关于心理咨询相关的问题，我都会尽力提供专业的帮助。请随时提出你的问题，我会尽力为你解答。期待你的咨询！\n",
      "\n",
      "\n",
      "=== 通义千问测试程序 ===\n",
      "1. 单轮对话测试\n",
      "2. 长文本对话测试\n",
      "3. 多轮对话测试\n",
      "0. 退出程序\n",
      "无效的选择，请重试\n",
      "\n",
      "=== 通义千问测试程序 ===\n",
      "1. 单轮对话测试\n",
      "2. 长文本对话测试\n",
      "3. 多轮对话测试\n",
      "0. 退出程序\n",
      "无效的选择，请重试\n",
      "\n",
      "=== 通义千问测试程序 ===\n",
      "1. 单轮对话测试\n",
      "2. 长文本对话测试\n",
      "3. 多轮对话测试\n",
      "0. 退出程序\n",
      "无效的选择，请重试\n",
      "\n",
      "=== 通义千问测试程序 ===\n",
      "1. 单轮对话测试\n",
      "2. 长文本对话测试\n",
      "3. 多轮对话测试\n",
      "0. 退出程序\n",
      "无效的选择，请重试\n",
      "\n",
      "=== 通义千问测试程序 ===\n",
      "1. 单轮对话测试\n",
      "2. 长文本对话测试\n",
      "3. 多轮对话测试\n",
      "0. 退出程序\n",
      "无效的选择，请重试\n",
      "\n",
      "=== 通义千问测试程序 ===\n",
      "1. 单轮对话测试\n",
      "2. 长文本对话测试\n",
      "3. 多轮对话测试\n",
      "0. 退出程序\n",
      "无效的选择，请重试\n",
      "\n",
      "=== 通义千问测试程序 ===\n",
      "1. 单轮对话测试\n",
      "2. 长文本对话测试\n",
      "3. 多轮对话测试\n",
      "0. 退出程序\n",
      "无效的选择，请重试\n",
      "\n",
      "=== 通义千问测试程序 ===\n",
      "1. 单轮对话测试\n",
      "2. 长文本对话测试\n",
      "3. 多轮对话测试\n",
      "0. 退出程序\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "from pathlib import Path\n",
    "\n",
    "# 测试基础配置\n",
    "BASE_URL = \"http://localhost:8000\"\n",
    "HEADERS = {\"Content-Type\": \"application/json\"}\n",
    "\n",
    "def test_qwen_chat():\n",
    "    \"\"\"测试通义千问单轮对话\"\"\"\n",
    "    url = f\"{BASE_URL}/QwenChat/\"\n",
    "    \n",
    "    # 获取用户输入\n",
    "    content = input(\"请输入您的问题: \")\n",
    "    system_role = input(\"请输入角色设定(直接回车使用默认'你是一名专业的心理医生'): \") or \"你是一名专业的心理医生\"\n",
    "    model = input(\"请输入模型名称(直接回车使用默认'deepseek-r1-distill-qwen-1.5b'): \") or \"deepseek-r1-distill-qwen-1.5b\"\n",
    "    \n",
    "    # 构建请求数据\n",
    "    payload = {\n",
    "        \"content\": content,\n",
    "        \"system_role\": system_role,\n",
    "        \"model\": model\n",
    "    }\n",
    "    \n",
    "    try:\n",
    "        print(\"\\n助手: \", end='', flush=True)\n",
    "        response = requests.post(url, data=payload)\n",
    "        for chunk in response.iter_content(chunk_size=None, decode_unicode=True):\n",
    "            if chunk:\n",
    "                print(chunk, end='', flush=True)\n",
    "        print(\"\\n\")\n",
    "        return True\n",
    "    except Exception as e:\n",
    "        print(f\"请求失败: {e}\")\n",
    "        return False\n",
    "\n",
    "def test_qwen_chat_file():\n",
    "    \"\"\"测试通义千问长文本对话\"\"\"\n",
    "    url = f\"{BASE_URL}/QwenChatFile/\"\n",
    "    \n",
    "    # 获取用户输入\n",
    "    file_path = input(\"请输入文件路径: \")\n",
    "    question = input(\"请输入您的问题: \")\n",
    "    # 长文本对话固定使用qwen-long模型\n",
    "    \n",
    "    try:\n",
    "        with open(file_path, 'rb') as f:\n",
    "            files = {'file': (Path(file_path).name, f)}\n",
    "            data = {'question': question}\n",
    "            \n",
    "            response = requests.post(url, files=files, data=data)\n",
    "            \n",
    "            if response.status_code == 200:\n",
    "                result = response.json()\n",
    "                print(\"\\n助手:\", result.get('response'))\n",
    "                return result\n",
    "            else:\n",
    "                print(f\"请求失败: {response.status_code}\")\n",
    "                print(f\"错误信息: {response.text}\")\n",
    "                return None\n",
    "    except Exception as e:\n",
    "        print(f\"请求失败: {e}\")\n",
    "        return None\n",
    "\n",
    "def test_qwen_chat_toke():\n",
    "    \"\"\"测试通义千问多轮对话\"\"\"\n",
    "    url = f\"{BASE_URL}/QwenChatToke/\"\n",
    "    session = requests.Session()\n",
    "    \n",
    "    # 获取模型名称\n",
    "    model = input(\"请输入模型名称(直接回车使用默认'qwen2.5-1.5b-instruct'): \") or \"qwen2.5-1.5b-instruct\"\n",
    "    print(\"开始多轮对话 (输入'退出'结束对话)\")\n",
    "    \n",
    "    while True:\n",
    "        user_input = input(\"\\n用户: \")\n",
    "        if user_input.lower() in ['退出', 'quit', 'exit', '0']:\n",
    "            break\n",
    "            \n",
    "        try:\n",
    "            response = session.post(\n",
    "                url, \n",
    "                data={\n",
    "                    \"content\": user_input,\n",
    "                    \"model\": model\n",
    "                }, \n",
    "                stream=True\n",
    "            )\n",
    "            \n",
    "            print(\"助手: \", end='', flush=True)\n",
    "            for chunk in response.iter_content(chunk_size=None, decode_unicode=True):\n",
    "                if chunk:\n",
    "                    print(chunk, end='', flush=True)\n",
    "            print()\n",
    "            \n",
    "        except Exception as e:\n",
    "            print(f\"请求失败: {e}\")\n",
    "            break\n",
    "\n",
    "def main():\n",
    "    while True:\n",
    "        print(\"\\n=== 通义千问测试程序 ===\")\n",
    "        print(\"1. 单轮对话测试\")\n",
    "        print(\"2. 长文本对话测试\")\n",
    "        print(\"3. 多轮对话测试\")\n",
    "        print(\"0. 退出程序\")\n",
    "        \n",
    "        choice = input(\"\\n请选择测试类型(0-3): \")\n",
    "        \n",
    "        if choice == '1':\n",
    "            test_qwen_chat()\n",
    "        elif choice == '2':\n",
    "            test_qwen_chat_file()\n",
    "        elif choice == '3':\n",
    "            test_qwen_chat_toke()\n",
    "        elif choice == '0':\n",
    "            break\n",
    "        else:\n",
    "            print(\"无效的选择，请重试\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Qwen 全模态理解Omni+OCR提取文字VL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'soundfile'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 7\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mbase64\u001b[39;00m\n\u001b[0;32m      6\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnumpy\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[1;32m----> 7\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01msoundfile\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01msf\u001b[39;00m\n\u001b[0;32m      8\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mpyaudio\u001b[39;00m\n\u001b[0;32m      9\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtime\u001b[39;00m\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'soundfile'"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "from pathlib import Path\n",
    "import re\n",
    "import base64\n",
    "import numpy as np\n",
    "import soundfile as sf\n",
    "import pyaudio\n",
    "import time\n",
    "\n",
    "# 测试基础配置\n",
    "BASE_URL = \"http://localhost:8000\"\n",
    "HEADERS = {\"Content-Type\": \"application/json\"}\n",
    "\n",
    "def play_audio(audio_string):\n",
    "    \"\"\"播放音频响应\"\"\"\n",
    "    if audio_string:\n",
    "        try:\n",
    "            wav_bytes = base64.b64decode(audio_string)\n",
    "            audio_np = np.frombuffer(wav_bytes, dtype=np.int16)\n",
    "            sf.write('audio_response.wav', audio_np, samplerate=24000)\n",
    "            \n",
    "            # 播放音频\n",
    "            p = pyaudio.PyAudio()\n",
    "            stream = p.open(format=pyaudio.paInt16, channels=1, rate=24000, output=True)\n",
    "            stream.write(audio_np.tobytes())\n",
    "            time.sleep(0.8)\n",
    "            stream.stop_stream()\n",
    "            stream.close()\n",
    "            p.terminate()\n",
    "        except Exception as e:\n",
    "            print(f\"音频播放失败: {e}\")\n",
    "\n",
    "def test_qwen_ocr():\n",
    "    \"\"\"测试通义千问OCR功能\"\"\"\n",
    "    url = f\"{BASE_URL}/QwenOCR/\"\n",
    "    \n",
    "    # 获取用户输入\n",
    "    file_path = input(\"请输入图片路径: \")\n",
    "    question = input(\"请输入问题（直接回车默认提取所有文字）: \") or \"提取所有图中文字\"\n",
    "    \n",
    "    try:\n",
    "        with open(file_path, 'rb') as f:\n",
    "            files = {'file': (Path(file_path).name, f)}\n",
    "            data = {'question': question}\n",
    "            \n",
    "            response = requests.post(url, files=files, data=data)\n",
    "            \n",
    "            if response.status_code == 200:\n",
    "                result = response.json()\n",
    "                print(\"\\n识别结果:\", result.get('response'))\n",
    "                return result\n",
    "            else:\n",
    "                print(f\"请求失败: {response.status_code}\")\n",
    "                print(f\"错误信息: {response.text}\")\n",
    "                return None\n",
    "    except Exception as e:\n",
    "        print(f\"请求失败: {e}\")\n",
    "        return None\n",
    "\n",
    "def test_qwen_omni():\n",
    "    \"\"\"测试通义千问多模态对话\"\"\"\n",
    "    url = f\"{BASE_URL}/Qwenomni/\"\n",
    "    session = requests.Session()  # 创建会话以保持对话历史\n",
    "    \n",
    "    # 获取语音选项\n",
    "    print(\"\\n=== 选择AI助手音色 ===\")\n",
    "    print(\"1. 邻家女孩 (Cherry)\")\n",
    "    print(\"2. 职场小美女 (Serena)\")\n",
    "    print(\"3. 邻家大哥哥 (Ethan)\")\n",
    "    print(\"4. 御姐 (Chelsie)\")\n",
    "    voice_choice = input(\"请选择 (1-4): \")\n",
    "    \n",
    "    voices = [\"Cherry\", \"Serena\", \"Ethan\", \"Chelsie\"]\n",
    "    if voice_choice not in ['1', '2', '3', '4']:\n",
    "        print(\"无效的选择\")\n",
    "        return None\n",
    "    \n",
    "    voice = voices[int(voice_choice) - 1]\n",
    "    \n",
    "    while True:  # 多轮对话循环\n",
    "        # 获取输入类型\n",
    "        print(\"\\n=== 选择输入类型 ===\")\n",
    "        print(\"1. 纯文本对话\")\n",
    "        print(\"2. 多媒体分析 (URL或本地文件)\")\n",
    "        choice = input(\"请选择功能 (1-2): \")\n",
    "        \n",
    "        try:\n",
    "            response = None\n",
    "            \n",
    "            if choice == '1':\n",
    "                text = input(\"请输入文本：\")\n",
    "                data = {\n",
    "                    'type': 'text',\n",
    "                    'text': text,\n",
    "                    'voice': voice\n",
    "                }\n",
    "                response = session.post(url, data=data, stream=True)\n",
    "                \n",
    "            elif choice == '2':\n",
    "                path = input(\"请输入URL或本地文件路径：\")\n",
    "                text = input(\"请输入相关问题（直接回车使用默认问题）：\") or \"请描述内容\"\n",
    "                \n",
    "                if is_url(path):\n",
    "                    # URL方式处理\n",
    "                    file_type = get_file_type(path)\n",
    "                    if not file_type:\n",
    "                        print(\"不支持的文件类型\")\n",
    "                        continue\n",
    "                        \n",
    "                    data = {\n",
    "                        'type': file_type,\n",
    "                        'text': text,\n",
    "                        'voice': voice,\n",
    "                        'url': path\n",
    "                    }\n",
    "                    response = session.post(url, data=data, stream=True)\n",
    "                else:\n",
    "                    # 本地文件处理\n",
    "                    file_type = get_file_type(path)\n",
    "                    if not file_type:\n",
    "                        print(\"不支持的文件类型\")\n",
    "                        continue\n",
    "                    \n",
    "                    try:\n",
    "                        with open(path, 'rb') as f:\n",
    "                            files = {'file': (Path(path).name, f.read())}\n",
    "                            data = {\n",
    "                                'type': file_type,\n",
    "                                'text': text,\n",
    "                                'voice': voice\n",
    "                            }\n",
    "                            response = session.post(url, data=data, files=files, stream=True)\n",
    "                    except IOError as e:\n",
    "                        print(f\"文件读取错误: {e}\")\n",
    "                        continue\n",
    "            else:\n",
    "                print(\"无效的选择\")\n",
    "                continue\n",
    "                \n",
    "            if not response:\n",
    "                print(\"请求未初始化\")\n",
    "                continue\n",
    "                \n",
    "            print(\"\\nAI助手: \", end='', flush=True)\n",
    "            \n",
    "            # 处理响应\n",
    "            if response.status_code == 200:\n",
    "                audio_string = \"\"\n",
    "                \n",
    "                for line in response.iter_lines(decode_unicode=True):\n",
    "                    if line:\n",
    "                        if line.startswith('audio:'):\n",
    "                            audio_string += line[6:]\n",
    "                        elif line.startswith('text:'):\n",
    "                            text_content = line[5:]\n",
    "                            print(text_content, end='', flush=True)\n",
    "                \n",
    "                print(\"\\n\")\n",
    "                # 播放音频响应\n",
    "                if audio_string:\n",
    "                    play_audio(audio_string)\n",
    "                \n",
    "                # 继续对话\n",
    "                while True:\n",
    "                    text = input(\"\\n请输入问题（或输入'q'退出）: \")\n",
    "                    if text.lower() == 'q':\n",
    "                        return\n",
    "                    \n",
    "                    # 发送纯文本对话请求\n",
    "                    data = {\n",
    "                        'type': 'text',\n",
    "                        'text': text,\n",
    "                        'voice': voice\n",
    "                    }\n",
    "                    response = session.post(url, data=data, stream=True)\n",
    "                    \n",
    "                    print(\"\\nAI助手: \", end='', flush=True)\n",
    "                    audio_string = \"\"\n",
    "                    \n",
    "                    for line in response.iter_lines(decode_unicode=True):\n",
    "                        if line:\n",
    "                            if line.startswith('audio:'):\n",
    "                                audio_string += line[6:]\n",
    "                            elif line.startswith('text:'):\n",
    "                                text_content = line[5:]\n",
    "                                print(text_content, end='', flush=True)\n",
    "                    \n",
    "                    print(\"\\n\")\n",
    "                    if audio_string:\n",
    "                        play_audio(audio_string)\n",
    "            else:\n",
    "                print(f\"请求失败: {response.status_code}\")\n",
    "                print(f\"错误信息: {response.text}\")\n",
    "                break\n",
    "                \n",
    "        except Exception as e:\n",
    "            print(f\"请求失败: {e}\")\n",
    "            break\n",
    "\n",
    "def is_url(string):\n",
    "    \"\"\"检查是否是URL\"\"\"\n",
    "    url_pattern = re.compile(\n",
    "        r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\\\(\\\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'\n",
    "    )\n",
    "    return bool(url_pattern.match(string))\n",
    "\n",
    "def get_file_type(path):\n",
    "    \"\"\"根据文件扩展名判断文件类型\"\"\"\n",
    "    ext = path.lower().split('.')[-1]\n",
    "    if ext in ['jpg', 'jpeg', 'png', 'gif']:\n",
    "        return 'image'\n",
    "    elif ext in ['mp3', 'wav', 'ogg']:\n",
    "        return 'audio'\n",
    "    elif ext in ['mp4', 'avi', 'mov']:\n",
    "        return 'video'\n",
    "    return None\n",
    "\n",
    "def main():\n",
    "    while True:\n",
    "        print(\"\\n=== 通义千问高级功能测试 ===\")\n",
    "        print(\"1. OCR文字识别测试\")\n",
    "        print(\"2. 多模态对话测试\")\n",
    "        print(\"0. 退出程序\")\n",
    "        \n",
    "        choice = input(\"\\n请选择测试类型(0-2): \")\n",
    "        \n",
    "        if choice == '1':\n",
    "            test_qwen_ocr()\n",
    "        elif choice == '2':\n",
    "            test_qwen_omni()\n",
    "        elif choice == '0':\n",
    "            break\n",
    "        else:\n",
    "            print(\"无效的选择，请重试\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Qwen 音频理解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "=== 通义千问功能测试 ===\n",
      "1. 音频理解测试\n",
      "0. 退出程序\n",
      "欢迎使用音频理解系统！\n",
      "您可以上传音频文件（URL或本地路径），并提出相关问题。\n",
      "\n",
      "模型回复：[{'text': '当然'}][{'text': '，'}][{'text': '生意'}][{'text': '好'}][{'text': '听'}][{'text': '。'}][]\n",
      "\n",
      "问题不能为空，请重新输入！\n",
      "问题不能为空，请重新输入！\n",
      "问题不能为空，请重新输入！\n",
      "问题不能为空，请重新输入！\n",
      "问题不能为空，请重新输入！\n",
      "请求失败: 400\n",
      "错误信息: {\"error\": \"\\u672a\\u63d0\\u4f9b\\u97f3\\u9891\\u6570\\u636e\"}\n",
      "音频文件不能为空，请重新输入！\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "from pathlib import Path\n",
    "import base64\n",
    "import re\n",
    "\n",
    "# 测试基础配置\n",
    "BASE_URL = \"http://localhost:8000\"\n",
    "HEADERS = {\"Content-Type\": \"application/json\"}\n",
    "\n",
    "def test_audio():\n",
    "    \"\"\"测试通义千问音频理解\"\"\"\n",
    "    url = f\"{BASE_URL}/QwenAudio/\"\n",
    "    session = requests.Session()  # 创建会话以保持对话历史\n",
    "    \n",
    "    print(\"欢迎使用音频理解系统！\")\n",
    "    print(\"您可以上传音频文件（URL或本地路径），并提出相关问题。\")\n",
    "    \n",
    "    while True:\n",
    "        # 1. 用户上传音频文件\n",
    "        audio_source = input(\"\\n请输入音频文件的URL或本地路径: \").strip()\n",
    "        if not audio_source:\n",
    "            print(\"音频文件不能为空，请重新输入！\")\n",
    "            continue\n",
    "        \n",
    "        # 2. 用户输入问题\n",
    "        question = input(\"请输入您想问的问题（如果为空，则默认总结音频内容）: \").strip()\n",
    "        \n",
    "        try:\n",
    "            if audio_source.startswith(('http://', 'https://')):\n",
    "                # URL方式\n",
    "                data = {\n",
    "                    'audio_source': audio_source,\n",
    "                    'question': question\n",
    "                }\n",
    "                response = session.post(url, data=data, stream=True)\n",
    "            else:\n",
    "                # 本地文件方式\n",
    "                try:\n",
    "                    with open(audio_source, 'rb') as f:\n",
    "                        files = {'file': (Path(audio_source).name, f)}\n",
    "                        data = {'question': question}\n",
    "                        response = session.post(url, data=data, files=files, stream=True)\n",
    "                except IOError as e:\n",
    "                    print(f\"文件读取错误: {e}\")\n",
    "                    continue\n",
    "            \n",
    "            # 处理响应\n",
    "            if response.status_code == 200:\n",
    "                print(\"\\n模型回复：\", end='', flush=True)\n",
    "                for line in response.iter_lines(decode_unicode=True):\n",
    "                    if line:\n",
    "                        if line.startswith('text:'):\n",
    "                            print(line[5:], end='', flush=True)\n",
    "                print(\"\\n\")\n",
    "                \n",
    "                # 多轮对话\n",
    "                while True:\n",
    "                    follow_up = input(\"\\n是否继续提问？（输入问题继续，输入'exit'结束对话）: \").strip()\n",
    "                    if follow_up.lower() == 'exit':\n",
    "                        return\n",
    "                    \n",
    "                    if follow_up:\n",
    "                        data = {\n",
    "                            'question': follow_up\n",
    "                        }\n",
    "                        response = session.post(url, data=data, stream=True)\n",
    "                        \n",
    "                        if response.status_code == 200:\n",
    "                            print(\"\\n模型回复：\", end='', flush=True)\n",
    "                            for line in response.iter_lines(decode_unicode=True):\n",
    "                                if line and line.startswith('text:'):\n",
    "                                    print(line[5:], end='', flush=True)\n",
    "                            print(\"\\n\")\n",
    "                        else:\n",
    "                            print(f\"请求失败: {response.status_code}\")\n",
    "                            print(f\"错误信息: {response.text}\")\n",
    "                            break\n",
    "                    else:\n",
    "                        print(\"问题不能为空，请重新输入！\")\n",
    "            else:\n",
    "                print(f\"请求失败: {response.status_code}\")\n",
    "                print(f\"错误信息: {response.text}\")\n",
    "                \n",
    "        except Exception as e:\n",
    "            print(f\"请求失败: {e}\")\n",
    "            continue\n",
    "\n",
    "def main():\n",
    "    while True:\n",
    "        print(\"\\n=== 通义千问功能测试 ===\")\n",
    "        print(\"1. 音频理解测试\")\n",
    "        print(\"0. 退出程序\")\n",
    "        \n",
    "        choice = input(\"\\n请选择测试类型(0-1): \")\n",
    "        \n",
    "        if choice == '1':\n",
    "            test_audio()\n",
    "        elif choice == '0':\n",
    "            break\n",
    "        else:\n",
    "            print(\"无效的选择，请重试\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 测试服务器地址"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import mysql.connector \n",
    "from mysql.connector  import Error\n",
    " \n",
    "def connect_to_database():\n",
    "    try:\n",
    "        # 配置数据库连接信息 \n",
    "        connection = mysql.connector.connect( \n",
    "            host=\"123.249.67.69\",       # 数据库服务器地址\n",
    "            port=3306,                 # 数据库端口（默认为 3306）\n",
    "            user=\"dailyfresh\",           # 数据库用户名\n",
    "            password=\"Yd011987..\",       # 数据库密码\n",
    "            database=\"dailyfresh\"    # 要连接的数据库名称 \n",
    "        )\n",
    " \n",
    "        if connection.is_connected(): \n",
    "            print(\"成功连接到数据库！\")\n",
    " \n",
    "            # 获取数据库版本信息 \n",
    "            db_info = connection.get_server_info() \n",
    "            print(f\"数据库版本: {db_info}\")\n",
    " \n",
    "            # 创建游标对象\n",
    "            cursor = connection.cursor() \n",
    " \n",
    "            # 执行 SQL 查询 \n",
    "            cursor.execute(\"SELECT  DATABASE();\")\n",
    "            record = cursor.fetchone() \n",
    "            print(f\"当前使用的数据库: {record[0]}\")\n",
    " \n",
    "    except Error as e:\n",
    "        print(f\"连接数据库时出错: {e}\")\n",
    " \n",
    "    finally:\n",
    "        # 关闭数据库连接\n",
    "        if connection.is_connected(): \n",
    "            cursor.close() \n",
    "            connection.close() \n",
    "            print(\"数据库连接已关闭。\")\n",
    " \n",
    "# 调用函数\n",
    "if __name__ == \"__main__\":\n",
    "    connect_to_database()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
